From 689dc2485095a3dce998a93ef364d6e652fff8a5 Mon Sep 17 00:00:00 2001 From: Nulo Date: Mon, 15 Nov 2021 18:48:48 -0300 Subject: [PATCH] Orders --- common.ts | 12 ++--- demo/demo.ts | 111 +++++++++++++++++++++++++++++++++++++- demo/index.html | 27 +++++++++- index.ts | 1 + order.ts | 138 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 280 insertions(+), 9 deletions(-) create mode 100644 order.ts diff --git a/common.ts b/common.ts index bc845af..a6d7d8e 100644 --- a/common.ts +++ b/common.ts @@ -56,10 +56,10 @@ export function queryCustomerToSearchParams( } export enum TipoDeDocumento { - CUIT = 80, - CUIL = 86, - CDI = 87, - LE = 89, - LC = 90, - DNI = 96, + CUIT = "80", + CUIL = "86", + CDI = "87", + LE = "89", + LC = "90", + DNI = "96", } diff --git a/demo/demo.ts b/demo/demo.ts index 2fa1b6f..d0982c7 100644 --- a/demo/demo.ts +++ b/demo/demo.ts @@ -4,7 +4,14 @@ import { getPrices, getPricesByCustomer, getCustomers, + order, + OrderDto, + TipoDeDocumento, + Customer, + Producto, + Precio, getPublications, + Publication, } from "../index.js"; const tokenInput = document.querySelector("#token-input")!; @@ -83,4 +90,106 @@ setupForm("#price", () => getPrices(token(), {})); setupForm("#price-by-customer", (event) => getPricesByCustomer(token(), { filter: (event.target! as any).filter.value }) ); -setupForm("#customer", () => getCustomers(token(), {})); +setupForm("#customers", () => getCustomers(token(), {})); + +let customer: Customer | null = null; + +setupForm("#pedido-customer", async (event) => { + const cuit = (event.target! as any).cuit.value; + if (cuit.length === 0) throw new Error("No pusiste un CUIT."); + + const customers = await getCustomers(token(), { + customer: { + type: TipoDeDocumento.CUIT, + number: cuit, + }, + }); + if (customers.Data.length !== 1) + throw new Error("Encontré más de unx cliente."); + customer = customers.Data[0]; + + return customers; +}); + +const productosEl = document.querySelector( + "#pedido-item > .productos" +)!; +let productos: { + producto: Producto; + precio: Precio; +}[] = []; +function renderProductosEl() { + clear(productosEl); + + for (const { producto, precio } of productos) { + const itemEl = document.createElement("li"); + itemEl.append( + `${producto.Description} (precio de cliente: $${precio.Price}, SKUCode: ${producto.SKUCode})` + ); + productosEl.append(itemEl); + } +} + +setupForm("#pedido-item", async (event) => { + if (!customer) throw new Error("No seteaste lx cliente todavía."); + const sku = (event.target! as any).sku.value; + if (sku.length === 0) throw new Error("No pusiste un SKU."); + + const productosResponse = await getProductos(token(), { filter: sku }); + if (productosResponse.Data.length !== 1) + throw new Error("Encontré más de un producto."); + + const preciosDelClienteResponse = await getPricesByCustomer(token(), { + SKUCode: sku, + customer: { + type: customer.DocumentType, + number: customer.DocumentNumber, + }, + }); + if (preciosDelClienteResponse.Data.length !== 1) + throw new Error("Encontré más de un producto."); + + productos.push({ + producto: productosResponse.Data[0], + precio: preciosDelClienteResponse.Data[0], + }); + renderProductosEl(); + + return productosResponse; +}); + +setupForm("#pedido", async (event) => { + if (!customer) throw new Error("No seteaste lx cliente todavía."); + + // Se supone que Total es: + // >=0 ∑[(OrderItems.Quantity x OrderItems.UnitPrice) – OrderItems.DiscountPorcentage)] + Shipping.ShippingCost + Principal.FinancialSurcharge – Principal.TotalDiscount + // Pero no tenemos la mayoría de las variables. + const total = productos.reduce((total, curr) => total + curr.precio.Price, 0); + + // Se supone que es único ¡pero lo tenemos que generar nosotrxs! wtf + const id = Math.floor(Math.random() * 100000).toString(); + + const orderJson: OrderDto = { + Date: new Date().toISOString(), + Total: total, + OrderID: id, + OrderNumber: id, + OrderItems: productos.map(({ producto, precio }) => ({ + Description: producto.Description, + UnitPrice: precio.Price, + Quantity: 1, + ProductCode: producto.SKUCode, + })), + Customer: { + CustomerID: 1, + User: "ADMIN", + IVACategoryCode: customer.IvaCategoryCode, + Email: "api@axoft.com", // En prod: customer.Email, tenemos que usar esto porque el usuario de prueba no tiene Email + ProvinceCode: customer.ProvinceCode, + DocumentNumber: customer.DocumentNumber, + DocumentType: customer.DocumentType, + }, + }; + + return await order(token(), orderJson); +}); diff --git a/demo/index.html b/demo/index.html index cc77607..615158d 100644 --- a/demo/index.html +++ b/demo/index.html @@ -5,6 +5,11 @@ form { border: solid 2px black; padding: 0.2em; + margin: 1em 0; + } + section { + border: solid 2px blue; + padding: 0.2em; margin-bottom: 1em; } @@ -54,9 +59,27 @@

-
- +

+
+
+
+

+ + +
+
+

+ + +
    +
    +
    +

    + +
    +
    + diff --git a/index.ts b/index.ts index 9549531..a995a97 100644 --- a/index.ts +++ b/index.ts @@ -5,4 +5,5 @@ export * from "./product.js"; export * from "./price.js"; export * from "./priceByCustomer.js"; export * from "./customer.js"; +export * from "./order.js"; export * from "./publications.js"; diff --git a/order.ts b/order.ts new file mode 100644 index 0000000..0384b5f --- /dev/null +++ b/order.ts @@ -0,0 +1,138 @@ +import { + HOST, + Paginacion, + paginacionToSearchParams, + Paging, +} from "./common.js"; + +export interface OrderDto { + SituacionOrden?: null | string; + Date: string; + Total: number; + TotalDiscount?: number; + PaidTotal?: number; + FinancialSurcharge?: number; + WarehouseCode?: null | string; + SellerCode?: null | string; + TransportCode?: null | string; + SaleConditionCode?: number; + InvoiceCounterfoil?: number; + PriceListNumber?: number; + AgreedWithSeller?: boolean; + IvaIncluded?: boolean; + InternalTaxIncluded?: boolean; + OrderID: string; + OrderNumber: string; + ValidateTotalWithPaidTotal?: boolean; + Comment?: null | string; + Customer: CustomerDto; + CancelOrder?: boolean; + OrderItems: OrderItemDto[]; + Shipping?: null | ShippingDto; + CashPayment?: null | CashPaymentDto; + CashPayments?: CashPaymentDto[] | null; + Payments?: PaymentDto[] | null; +} +export interface CustomerDto { + CustomerID: number; + Code?: null | string; + DocumentType?: null | string; + DocumentNumber?: null | string; + IVACategoryCode: string; + PayInternalTax?: boolean; + User: string; + Email: string; + FirstName?: null | string; + LastName?: null | string; + BusinessName?: null | string; + Street?: null | string; + HouseNumber?: null | string; + Floor?: null | string; + Apartment?: null | string; + City?: null | string; + ProvinceCode: string; + PostalCode?: null | string; + PhoneNumber1?: null | string; + PhoneNumber2?: null | string; + Bonus?: number; + MobilePhoneNumber?: null | string; + WebPage?: null | string; + BusinessAddress?: null | string; + Comments?: null | string; + NumberListPrice?: number; + Removed?: boolean; + DateUpdate?: string; + Disable?: string; +} +export interface OrderItemDto { + ProductCode: string; + SKUCode?: null | string; + VariantCode?: null | string; + Description: string; + VariantDescription?: null | string; + Quantity: number; + UnitPrice: number; + DiscountPercentage?: number; +} +export interface ShippingDto { + ShippingID: number; + ShippingCode?: null | string; + Street?: null | string; + HouseNumber?: null | string; + Floor?: null | string; + Apartment?: null | string; + City?: null | string; + ProvinceCode?: null | string; + PostalCode?: null | string; + PhoneNumber1?: null | string; + PhoneNumber2?: null | string; + ShippingCost?: number; + DeliversMonday?: null | string; + DeliversTuesday?: null | string; + DeliversWednesday?: null | string; + DeliversThursday?: null | string; + DeliversFriday?: null | string; + DeliversSaturday?: null | string; + DeliversSunday?: null | string; + DeliveryHours?: null | string; +} +export interface CashPaymentDto { + PaymentID?: number; + PaymentMethod: string; + PaymentTotal?: number; +} +export interface PaymentDto { + PaymentId: number; + TransactionDate: string; + AuthorizationCode?: null | string; + TransactionNumber?: null | string; + Installments: number; + InstallmentAmount: number; + Total: number; + CardCode: string; + CardPlanCode: string; + VoucherNo: number; + CardPromotionCode?: null | string; +} + +export interface OrderResponse {} + +export async function order( + token: string, + order: OrderDto +): Promise { + const res = await fetch(`${HOST}/api/Aperture/order`, { + method: "POST", + headers: { + accesstoken: token, + "content-type": "application/json", + }, + body: JSON.stringify(order), + }); + const json = await res.json(); + console.debug(json); + if (!json.isOk) { + throw new Error(`Tango: ${json.Message}`); + } + return json; +}