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 CartSetSkuPiece(CartSetSkuPieceRequest) 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 CartSetOwner(CartSetOwnerRequest) returns (CartObject);
  rpc CartSetStore(CartSetStoreRequest) returns (CartObject);
  rpc CartClose(CartCloseRequest) returns (CartObject);
  rpc CartRemove(CartRemoveRequest) returns (google.protobuf.Empty);

  rpc CartLoyaltyCardAdd(LoyaltyCardAddRequest) returns (CartObject);
  rpc CartLoyaltyCardRemove(LoyaltyCardRemoveRequest) returns (CartObject);
  rpc CartBurnPoints(BurnPointsRequest) returns (CartObject);
  rpc CartCommitmentAdd(AddCommitmentRequest) returns (CartObject);
  rpc CartCommitmentRemove(RemoveCommitmentRequest) returns (CartObject);

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

message e {}

message LoyaltyCardAddRequest {
  string cart_id = 1;
  string account_id = 2;
  string card_id = 3;
  string loyalty_level = 4;
}

message LoyaltyCardRemoveRequest { string cart_id = 1; }

message PurchaseSetInvoiceIdRequest {
  string purchase_id = 1;
  string invoice_id = 2;
}

message BurnPointsRequest {
  string cart_id = 1;
  string loyalty_account_id = 2;
  string transaction_id = 3;
  int32 points_to_burn = 4;
}

message AddCommitmentRequest {
  string cart_id = 1;
  string commitment_id = 2;
  uint32 discount_percentage = 3;
}

message RemoveCommitmentRequest {
  string cart_id = 1;
  string commitment_id = 2;
}

message CartIds { repeated string cart_ids = 1; }

message CartInfoObject {
  string cart_id = 1;
  string customer_name = 2;
  uint32 upl_count = 3;
  repeated string item_names = 4;
  uint32 owner = 5;
  uint32 created_by = 6;
  string created_at = 7;
}

message CartSetSkuPieceRequest {
  string cart_id = 1;
  uint32 sku = 2;
  uint32 piece = 3;
}

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

message CartSetOwnerRequest {
  string cart_id = 1;
  uint32 owner_uid = 2;
}

message CartSetStoreRequest {
  string cart_id = 1;
  uint32 store_id = 2;
}

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_zip = 4;
  string customer_location = 5;
  string customer_street = 6;
  string tax_number = 7;
}

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 zip = 3;
  string location = 4;
  string street = 5;
  string tax_number = 6;
}

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

message LoyaltyTransaction {
  string loyalty_account_id = 1;
  string transaction_id = 2;
  int32 burned_points = 3;
}

message CartObject {
  message Item {
    uint32 sku = 1;
    string name = 2;
    uint32 piece = 3;
    uint32 retail_price_net = 6;
    string vat = 7;
    uint32 retail_price_gross = 8;
    uint32 total_retail_price_net = 9;
    uint32 total_retail_price_gross = 10;
  }
  string ancestor = 1;
  string id = 2;
  Customer customer = 3;
  string commitment_id = 4;
  uint32 commitment_discount_percentage = 5;
  string loyalty_card_id = 6;
  string loyalty_account_id = 7;
  string loyalty_level = 8;
  repeated Item shopping_list = 9;
  repeated UplInfoObject upls_sku = 10;
  repeated UplInfoObject upls_unique = 11;
  uint32 total_net = 12;
  uint32 total_vat = 13;
  uint32 total_gross = 14;
  uint32 commitment_discount_amount_gross = 15;
  uint32 burned_loyalty_points = 16;
  bool need_invoice = 17;
  PaymentKind payment_kind = 18;
  repeated Payment payments = 19;
  repeated LoyaltyTransaction burned_points = 20;
  int32 payable = 21;
  int32 payment_balance = 22;
  int32 profit_net = 23;
  uint32 owner_uid = 24;
  uint32 store_id = 25;
  string date_completion = 26;
  string payment_duedate = 27;
  uint32 created_by = 28;
  string created_at = 29;
}

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;
  uint32 retail_net_price = 5;
  string vat = 6;
  uint32 retail_gross_price = 7;
  uint32 procurement_net_price = 8;
  string best_before = 9;
  bool depreciated = 10;
}

// 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;
  int32 balance = 7;
  int32 payable = 8;
  bool document_invoice = 9;
  string invoice_id = 17;
  string date_completion = 10;
  string payment_duedate = 11;
  bool payment_expired = 12;
  int32 profit_net = 13;
  bool restored = 14;
  uint32 created_by = 15;
  string created_at = 16;
  // 18
}

message PurchaseObject {
  enum ItemKind {
    Sku = 0;
    DerivedProduct = 1;
    DepreciatedSku = 2;
  }
  message Item {
    ItemKind kind = 1;
    uint32 product_id = 2;
    string name = 3;
    uint32 piece = 4;
    uint32 retail_price_net = 5;
    string vat = 6;
    uint32 retail_price_gross = 7;
    uint32 total_retail_price_net = 8;
    uint32 total_retail_price_gross = 9;
    repeated string upl_ids = 10;
  }
  string id = 1;
  Customer customer = 2;
  string commitment_id = 3;
  uint32 commitment_discount_percentage = 4;
  uint32 commitment_discount_amount_gross = 5;
  string loyalty_card_id = 6;
  string loyalty_account_id = 7;
  string loyalty_level = 8;
  uint32 burned_loyalty_points = 9;
  repeated Item items = 10;
  repeated UplInfoObject upl_info_objects = 11;
  bool need_invoice = 12;
  string invoice_id = 29;
  uint32 total_net = 13;
  uint32 total_vat = 14;
  uint32 total_gross = 15;
  PaymentKind payment_kind = 16;
  repeated Payment payments = 17;
  repeated LoyaltyTransaction burned_points = 18;
  int32 payable = 19;
  int32 payment_balance = 20;
  int32 profit_net = 21;
  uint32 owner_uid = 22;
  uint32 store_id = 23;
  string date_completion = 24;
  string payment_duedate = 25;
  bool restored = 26;
  uint32 created_by = 27;
  string created_at = 28;
  // 30
}

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;
}