protected virtual IEnumerable LumCallDCLShipemnt(PXAdapter adapter, [PXDate] DateTime?shipDate, [PXInt] int?siteID, [SOOperation.List] string operation) { try { var _soOrder = adapter.Get <SOOrder>().ToList()[0]; // Get DCL SO. Data(正式:order_number = SO.OrderNbr) //var dclOrders = JsonConvert.DeserializeObject<OrderResponse>( // DCLHelper.CallDCLToGetSOByOrderNumbers( // this.DCLSetup.Select().RowCast<LUMVendCntrlSetup>().FirstOrDefault(), soOrder.OrderNbr).ContentResult); // Get DCL SO. Data(理論上資料一定存在,因為Process Order已經先篩選了) var dclOrders = JsonConvert.DeserializeObject <OrderResponse>( DCLHelper.CallDCLToGetSOByOrderNumbers( this.DCLSetup.Select().RowCast <LUMVendCntrlSetup>().FirstOrDefault(), _soOrder.CustomerRefNbr).ContentResult); if (dclOrders.orders == null) { throw new Exception("Can not Mapping DCL Data"); } if (!dclOrders.orders.Any(x => x.order_stage == 60)) { throw new Exception("DCL Order stage is not Fully Shipped"); } if (_soOrder.OrderType == "FM") { var setup = this.MiddlewareSetup.Select().RowCast <LUMMiddleWareSetup>().FirstOrDefault(); var shippingCarrier = dclOrders.orders.FirstOrDefault()?.shipping_carrier; var packagesInfo = dclOrders.orders.FirstOrDefault().shipments.SelectMany(x => x.packages); var _merchant = string.IsNullOrEmpty(PXAccess.GetCompanyName()?.Split(' ')[1]) ? "us" : PXAccess.GetCompanyName()?.Split(' ')[1].ToLower() == "uk" ? "gb" : PXAccess.GetCompanyName()?.Split(' ')[1].ToLower(); MiddleWare_Shipment metaData = new MiddleWare_Shipment() { merchant = _merchant, amazon_order_id = dclOrders.orders.FirstOrDefault().po_number, shipment_date = dclOrders.orders.FirstOrDefault()?.shipments?.FirstOrDefault()?.ship_date + " 00:00:00", shipping_method = "Standard", carrier = shippingCarrier, tracking_number = string.Join("|", packagesInfo.Select(x => x.tracking_number)) }; // Update FBM var updateResult = MiddleWareHelper.CallMiddleWareToUpdateFBM(setup, metaData); // Check HttpStatusCode if (updateResult.StatusCode != System.Net.HttpStatusCode.OK) { throw new PXException($"Update MiddleWare FBM fail , Code = {updateResult.StatusCode}"); } // Check Response status var updateModel = JsonConvert.DeserializeObject <MiddleWare_Response>(updateResult.ContentResult); if (!updateModel.Status) { throw new PXException($"Update Middleware FBM fail, Msg = {updateModel.Message}"); } _soOrder.GetExtension <SOOrderExt>().UsrSendToMiddleware = true; Base.Document.Update(_soOrder); Base.Save.Press(); } else { using (PXTransactionScope sc = new PXTransactionScope()) { Base.CreateShipmentIssue(adapter, shipDate, siteID); var processResult = PXProcessing <SOOrder> .GetItemMessage(); if (processResult.ErrorLevel != PXErrorLevel.RowInfo) { return(adapter.Get()); } // Create SOShipment Graph var graph = PXGraph.CreateInstance <SOShipmentEntry>(); // Find SOShipment var _soOrderShipments = FbqlSelect <SelectFromBase <SOOrderShipment, TypeArrayOf <IFbqlJoin> .Empty> .Where <BqlChainableConditionBase <TypeArrayOf <IBqlBinary> .FilledWith <And <Compare <SOOrderShipment.orderType, Equal <P.AsString> > > > > .And <BqlOperand <SOOrderShipment.orderNbr, IBqlString> .IsEqual <P.AsString> > >, SOOrderShipment> .View.Select(Base, _soOrder.OrderType, _soOrder.OrderNbr) .RowCast <SOOrderShipment>(); foreach (var refItem in _soOrderShipments) { // Create new Adapter var newAdapter = new PXAdapter(graph.Document) { Searches = new Object[] { refItem.ShipmentNbr } }; // Select Current Shipment var _soShipment = newAdapter.Get <SOShipment>().ToList()[0]; try { // Get Carrier and TrackingNbr var shippingCarrier = dclOrders.orders.FirstOrDefault().shipping_carrier; var packagesInfo = dclOrders.orders.FirstOrDefault().shipments.SelectMany(x => x.packages); _soShipment.GetExtension <SOShipmentExt>().UsrCarrier = shippingCarrier; _soShipment.GetExtension <SOShipmentExt>().UsrTrackingNbr = string.Join("|", packagesInfo.Select(x => x.tracking_number)); _soShipment.ShipmentDesc = $"Carrier: {shippingCarrier}|" + $"TrackingNbr: {string.Join("|", packagesInfo.Select(x => x.tracking_number))}"; if (_soShipment.ShipmentDesc.Length > 256) { _soShipment.ShipmentDesc = _soShipment.ShipmentDesc.Substring(0, 255); } } catch (Exception e) { _soShipment.ShipmentDesc = e.Message; } // Update Data graph.Document.Update(_soShipment); // Remove Hold graph.releaseFromHold.PressButton(newAdapter); // Confirm Shipment graph.confirmShipmentAction.PressButton(newAdapter); // Prepare Invoice For 3D Orders try { if (_soOrder.OrderType == "3D") { newAdapter.AllowRedirect = true; graph.createInvoice.PressButton(newAdapter); } } catch (PXRedirectRequiredException ex) { SOInvoiceEntry invoiceEntry = ex.Graph as SOInvoiceEntry; var soTax = SelectFrom <SOTaxTran> .Where <SOTaxTran.orderNbr.IsEqual <P.AsString> .And <SOTaxTran.orderType.IsEqual <P.AsString> > > .View.Select(Base, _soOrder.OrderNbr, _soOrder.OrderType) .RowCast <SOTaxTran>().FirstOrDefault(); var balance = invoiceEntry.Document.Current.CuryDocBal; var refNbr = invoiceEntry.Document.Current.RefNbr; var invTax = SelectFrom <ARTaxTran> .Where <ARTaxTran.refNbr.IsEqual <P.AsString> > .View.Select(Base, refNbr).RowCast <ARTaxTran>().FirstOrDefault(); var adjd = SelectFrom <ARAdjust2> .Where <ARAdjust2.adjdRefNbr.IsEqual <P.AsString> > .View.Select(Base, refNbr).RowCast <ARAdjust2>().FirstOrDefault(); if (soTax != null) { // setting Tax invoiceEntry.Taxes.SetValueExt <ARTaxTran.curyTaxAmt>(invTax, soTax.CuryTaxAmt); invoiceEntry.Taxes.Update(invTax); // setting Document invoiceEntry.Document.SetValueExt <ARInvoice.curyTaxTotal>(invoiceEntry.Document.Current, soTax.CuryTaxAmt); invoiceEntry.Document.SetValueExt <ARInvoice.curyDocBal>(invoiceEntry.Document.Current, balance + (soTax.CuryTaxAmt ?? 0)); invoiceEntry.Document.Update(invoiceEntry.Document.Current); invoiceEntry.Adjustments.SetValueExt <ARAdjust2.curyAdjdAmt>(adjd, adjd.CuryAdjdAmt + (soTax.CuryTaxAmt ?? 0)); invoiceEntry.Adjustments.Update(adjd); invoiceEntry.releaseFromCreditHold.Press(); invoiceEntry.Save.Press(); } } catch (Exception ex) { throw new Exception(ex.Message); } } sc.Complete(); } } } catch (Exception e) { PXProcessing.SetError <SOOrder>(e.Message); } return(adapter.Get()); }
protected virtual IEnumerable LumGenerate3PLUKFile(PXAdapter adapter, [PXDate] DateTime?shipDate, [PXInt] int?siteID, [SOOperation.List] string operation) { try { // Create SOShipment Graph var graph = PXGraph.CreateInstance <SOShipmentEntry>(); var soOrder = adapter.Get <SOOrder>().FirstOrDefault(); using (PXTransactionScope sc = new PXTransactionScope()) { // FBM wont create Shipment, only upload file to FTP if (soOrder.OrderType == "FM") { // Combine csv data var result = graph.GetExtension <SOShipmentEntryExt>().CombineCSVForFBM(soOrder, "P3PL"); // Upload Graph UploadFileMaintenance upload = PXGraph.CreateInstance <UploadFileMaintenance>(); // Create SM.FileInfo var fileName = $"{soOrder.OrderNbr}.csv"; var data = new UTF8Encoding(true).GetBytes(result.csvText.ToString()); FileInfo fi = new FileInfo(fileName, null, data); // upload file to FTP #region 3PL UK FTP var configYusen = SelectFrom <LUM3PLUKSetup> .View.Select(Base).RowCast <LUM3PLUKSetup>().FirstOrDefault(); FTP_Config config = new FTP_Config() { FtpHost = configYusen.FtpHost, FtpUser = configYusen.FtpUser, FtpPass = configYusen.FtpPass, FtpPort = configYusen.FtpPort, FtpPath = configYusen.FtpPath }; var ftpResult = graph.GetExtension <SOShipmentEntryExt>().UploadFileByFTP(config, fileName, data); //var ftpResult = true; if (!ftpResult) { throw new Exception("Ftp Upload Fail!!"); } #endregion // upload file to Attachment upload.SaveFile(fi); PXNoteAttribute.SetFileNotes(Base.Document.Cache, Base.Document.Current, fi.UID.Value); Base.Save.Press(); PXProcessing.SetProcessed(); } else { // Create Shipment Base.CreateShipmentIssue(adapter, shipDate, siteID); if (PXProcessing <SOOrder> .GetItemMessage().ErrorLevel != PXErrorLevel.RowInfo) { return(null); } // Find SOShipment var _soOrderShipment = FbqlSelect <SelectFromBase <SOOrderShipment, TypeArrayOf <IFbqlJoin> .Empty> .Where <BqlChainableConditionBase <TypeArrayOf <IBqlBinary> .FilledWith <And <Compare <SOOrderShipment.orderType, Equal <P.AsString> > > > > .And <BqlOperand <SOOrderShipment.orderNbr, IBqlString> .IsEqual <P.AsString> > >, SOOrderShipment> .View.Select(Base, soOrder.OrderType, soOrder.OrderNbr) .RowCast <SOOrderShipment>().FirstOrDefault(); // Create new Adapter var newAdapter = new PXAdapter(graph.Document) { Searches = new Object[] { _soOrderShipment.ShipmentNbr } }; // Generate UK csv file and upload to FTP graph.GetExtension <SOShipmentEntryExt>().lumGenerate3PLUKFile.PressButton(newAdapter); // Remove Hold graph.releaseFromHold.PressButton(newAdapter); } if (PXProcessing <SOOrder> .GetItemMessage().ErrorLevel == PXErrorLevel.RowInfo) { sc.Complete(); } } } catch (Exception ex) { PXProcessing.SetError <SOOrder>(ex.Message); } return(adapter.Get()); }