syntax = "proto3";
package purchase;
import "google/protobuf/empty.proto";

service Purchase {
  rpc CartNew(CartNewRequest) returns (CartObject);
  rpc CartGetAll(google.protobuf.Empty) returns (CartIds);
  rpc CartGetById(CartByIdRequest) returns (CartObject);
  rpc CartGetInfoBulk(CartBulkRequest) returns (stream CartInfoObject);
  rpc CartAddCustomer(CartAddCustomerReuqest) returns (CartObject);
  rpc CartRemoveCustomer(CartRemoveCustomerRequest) returns (CartObject);
  rpc CartAddSku(CartAddSkuRequest) returns (CartObject);
  rpc CartRemoveSku(CartRemoveSkuRequest) returns (CartObject);
  rpc CartAddUpl(CartAddUplRequest) returns (CartObject);
  rpc CartRemoveUpl(CartRemoveUplRequest) returns (CartObject);
  rpc CartSetDocument(CartSetDocumentRequest) returns (CartObject);
  rpc CartSetPayment(CartSetPaymentRequest) returns (CartObject);
  rpc CartAddPayment(CartAddPaymentRequest) returns (CartObject);
  rpc CartClose(CartCloseRequest) returns (CartObject);
  rpc CartRemove(CartRemoveRequest) returns (google.protobuf.Empty);

  rpc PurchaseGetById(PurchaseByIdRequest) returns (PurchaseObject);
  rpc PurchaseGetAll(google.protobuf.Empty) returns (PurchaseIds);
  rpc PurchaseGetInfoBulk(PurchaseBulkRequest)
      returns (stream PurchaseInfoObject);
  rpc PuchaseCreateInvoice(PurchaseCreateInvoiceRequest)
      returns (PurchaseObject);
  rpc PurchaseAddPayment(PurchaseAddPaymentRequest) returns (PurchaseObject);
  rpc PurchaseRestore(PurchaseRestoreRequest) returns (PurchaseObject);
  rpc PurchaseGetStatByInterval(e) returns (PurchaseStatResponse);
}

message e {}

message CartIds { repeated string cart_ids = 1; }

message CartInfoObject {}

message CartNewRequest {
  uint32 store_id = 1;
  uint32 owner_id = 2;
  uint32 created_by = 3;
}

message CartByIdRequest { string cart_id = 1; }

message CartBulkRequest { repeated string cart_ids = 1; }

message CartAddCustomerReuqest {
  string cart_id = 1;
  uint32 customer_id = 2;
  string customer_name = 3;
  string customer_address = 4;
  string tax_number = 5;
}

message CartRemoveCustomerRequest { string cart_id = 1; }

enum DocumentKind {
  Receipt = 0;
  Invoice = 1;
}

enum PaymentKind {
  Cash = 0;
  Card = 1;
  Transfer = 2;
}

message CartAddSkuRequest {
  string cart_id = 1;
  uint32 sku_id = 2;
  uint32 piece = 3;
  string name = 4;
  string vat = 5;
  uint32 retail_price_net = 6;
  uint32 retail_price_gross = 7;
}

message CartRemoveSkuRequest {
  string cart_id = 1;
  uint32 sku_id = 2;
}

message CartAddUplRequest {
  string cart_id = 1;
  UplInfoObject upl = 2;
}

message CartRemoveUplRequest {
  string cart_id = 1;
  string upl_id = 2;
}

message CartSetDocumentRequest {
  string cart_id = 1;
  DocumentKind document_kind = 2;
}

message CartSetPaymentRequest {
  string cart_id = 1;
  PaymentKind payment_kind = 2;
}

message CartAddPaymentRequest {
  string cart_id = 1;
  string payment_id = 2;
  int32 amount = 3;
}

message CartCloseRequest { string cart_id = 1; }

message CartRemoveRequest { string cart_id = 1; }

message Customer {
  uint32 customer_id = 1;
  string name = 2;
  string address = 3;
  string tax_number = 4;
}

message Payment {
  string payment_id = 1;
  int32 amount = 2;
}

message CartObject {
  enum ItemKind {
    Sku = 0;
    DerivedProduct = 1;
    DepreciatedSku = 2;
  }
  message Item {
    ItemKind kind = 1;
    string name = 2;
    uint32 piece = 3;
    uint32 retail_price_net = 4;
    string vat = 5;
    uint32 retail_price_gross = 6;
    uint32 total_retail_price_net = 7;
    uint32 total_retail_price_gross = 8;
    repeated string upl_ids = 9;
  }
  string ancestor = 1;
  string id = 2;
  Customer customer = 3;
  repeated Item items = 4;
  repeated UplInfoObject upl_info_objects = 5;
  bool need_invoice = 6;
  PaymentKind payment_kind = 7;
  repeated Payment payments = 8;
  int32 payment_balance = 9;
  uint32 profit_net = 10;
  uint32 owner_uid = 11;
  uint32 store_id = 12;
  uint32 created_by = 13;
  string created_at = 14;
}

message UplInfoObject {
  message UplKindSku {
    uint32 sku = 1;
    uint32 piece = 2;
  }
  message UplKindOpenedSku {
    uint32 product_id = 1;
    uint32 amount = 2;
  }

  string upl_id = 1;
  oneof upl_kind {
    UplKindSku sku = 2;
    UplKindOpenedSku opened_sku = 3;
  }
  string name = 4;
  string unit = 5;
  uint32 retail_net_price = 6;
  string vat = 7;
  uint32 retail_gross_price = 8;
  uint32 procurement_net_price = 9;
  string best_before = 10;
  bool depreciated = 11;
}

// PURCHASE

message PurchaseInfoObject {
  string purchase_id = 1;
  Customer customer = 2;
  uint32 upl_count = 3;
  uint32 total_net_price = 4;
  uint32 total_vat = 5;
  uint32 total_gross_price = 6;
  uint32 balance = 7;
  bool document_invoice = 8;
  string payment_duedate = 9;
  bool payment_expired = 10;
  uint32 profit_net = 11;
  bool restored = 12;
  uint32 created_by = 14;
  string created_at = 15;
}

message PurchaseObject {
  enum ItemKind {
    Sku = 0;
    DerivedProduct = 1;
    DepreciatedSku = 2;
  }
  message Item {
    ItemKind kind = 1;
    string name = 2;
    uint32 piece = 3;
    uint32 retail_price_net = 4;
    string vat = 5;
    uint32 retail_price_gross = 6;
    uint32 total_retail_price_net = 7;
    uint32 total_retail_price_gross = 8;
    repeated string upl_ids = 9;
  }
  string id = 1;
  Customer customer = 2;
  repeated Item items = 3;
  repeated UplInfoObject upl_info_objects = 4;
  bool need_invoice = 5;
  PaymentKind payment_kind = 6;
  repeated Payment payments = 7;
  int32 payment_balance = 8;
  uint32 profit_net = 9;
  uint32 owner_uid = 10;
  uint32 store_id = 11;
  bool closed = 12;
  uint32 created_by = 13;
  string created_at = 14;
}

message PurchaseIds { repeated string purchase_ids = 1; }

message PurchaseByIdRequest { string purchase_id = 1; }

message PurchaseBulkRequest { repeated string purchase_ids = 1; }

message PurchaseCreateInvoiceRequest { string purchase_id = 1; }

message PurchaseAddPaymentRequest {
  string purchase_id = 1;
  Payment payment = 2;
}

message PurchaseRestoreRequest { string purchase_id = 1; }

message PurchaseStatIntervalRequest {
  string from_rfc3339 = 1;
  string till_rfc3339 = 2;
}

message PurchaseStatResponse {
  message PurchaseStat {
    string date = 1;
    uint32 total_purchase_value_huf = 2;
    uint32 total_purchase_count = 3;
  }
  repeated PurchaseStat stats = 1;
}