예제 #1
0
        /// <summary> Update Shipment Tracking Info </summary>
        public virtual bool UpdateShipmentTrackingInfo(List <LUMWarehouseImportProcess> warehouseData)
        {
            var isAllSuccess = true;
            var logData      = SelectFrom <LUMWarehouseImportProcessLog> .View.Select(this).RowCast <LUMWarehouseImportProcessLog>();

            foreach (var row in warehouseData)
            {
                try
                {
                    var _soOrder = SelectFrom <SOOrder> .Where <SOOrder.orderNbr.IsEqual <P.AsString> > .View.Select(this, row.Erporder).RowCast <SOOrder>()?.FirstOrDefault();

                    #region Check is already updated

                    if (logData.Any(x => x.Erporder == row.Erporder && x.ShipmentID == row.ShipmentID && (x.IsProcess ?? false)))
                    {
                        //this.ImportShipmentList.Delete(row);
                        continue;
                    }

                    #endregion

                    #region Check SOOrder is Exists

                    if (_soOrder == null)
                    {
                        throw new Exception("SOOrder is not exists");
                    }

                    #endregion

                    #region Check data

                    if (string.IsNullOrEmpty(row.Erporder) || row.Erporder == "null")
                    {
                        throw new Exception("ERP Order Nbr can not be empty!");
                    }
                    if (string.IsNullOrEmpty(row.Carrier) || row.Carrier == "null")
                    {
                        throw new Exception("Carrier can not be empty!");
                    }
                    if (!row.ShipmentDate.HasValue)
                    {
                        throw new Exception("Shipment Date can not be empty!");
                    }
                    if (string.IsNullOrEmpty(row.TrackingNbr) || row.TrackingNbr == "null")
                    {
                        throw new Exception("Tracking Nbr can not be empty!");
                    }

                    #endregion

                    if (_soOrder.OrderType == "FM")
                    {
                        var setup           = this.MiddlewareSetup.Select().RowCast <LUMMiddleWareSetup>().FirstOrDefault();
                        var shippingCarrier = row.Carrier;
                        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 = _soOrder.CustomerOrderNbr,
                            shipment_date   = row.ShipmentDate?.ToString("yyyy-MM-dd hh:mm:ss"),
                            shipping_method = "Standard",
                            carrier         = shippingCarrier,
                            tracking_number = row.TrackingNbr
                        };
                        // 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}");
                        }
                        // update SOOrder_UsrSendToMiddleware
                        var soorderGraph = PXGraph.CreateInstance <SOOrderEntry>();
                        _soOrder.GetExtension <SOOrderExt>().UsrSendToMiddleware = true;
                        soorderGraph.Document.Update(_soOrder);
                        soorderGraph.Save.Press();
                        InsertLog(row, true, string.Empty);
                    }
                    else
                    {
                        var _soOrderShipment = SelectFrom <SOOrderShipment>
                                               .Where <SOOrderShipment.orderNbr.IsEqual <P.AsString>
                                                       .And <SOOrderShipment.orderType.IsEqual <P.AsString> > >
                                               .View.Select(this, _soOrder.OrderNbr, _soOrder.OrderType).RowCast <SOOrderShipment>()?.FirstOrDefault();

                        #region Check SOOrderShipment is Exists

                        if (_soOrderShipment == null)
                        {
                            throw new Exception("SOOrder need to create Shipment first!");
                        }

                        #endregion

                        var _soShipment = SelectFrom <SOShipment> .Where <SOShipment.shipmentNbr.IsEqual <P.AsString> >
                                          .View.Select(this, _soOrderShipment?.ShipmentNbr).RowCast <SOShipment>().FirstOrDefault();

                        #region Check SOShipment is Exists

                        if (_soShipment == null)
                        {
                            throw new Exception("Shipment is not exists!");
                        }

                        #endregion

                        // Update Shipment
                        var shipmentGraph = PXGraph.CreateInstance <SOShipmentEntry>();
                        _soShipment.GetExtension <SOShipmentExt>().UsrTrackingNbr = row.TrackingNbr;
                        _soShipment.GetExtension <SOShipmentExt>().UsrCarrier     = row.Carrier;
                        shipmentGraph.Document.Update(_soShipment);
                        shipmentGraph.Save.Press();

                        InsertLog(row, true, string.Empty);
                    }
                }
                catch (Exception ex)
                {
                    isAllSuccess = false;
                    InsertLog(row, false, ex.Message);
                }
            }
            return(isAllSuccess);
        }
예제 #2
0
        public static LumAPIResultModel CallMiddleWareToUpdateFBM(LUMMiddleWareSetup setup, MiddleWare_Shipment metadataShipemt)
        {
            // Get Middle ware Token
            var tokenResult = MiddleWareHelper.GetToken(setup);

            if (tokenResult.StatusCode != System.Net.HttpStatusCode.OK)
            {
                throw new Exception($"Can not Get Midlle Ware Token, status code:{tokenResult.StatusCode} ");
            }
            setup.AuthToken = JsonConvert.DeserializeObject <MiddleWareSingInModel>(tokenResult.ContentResult).data.Jwt;

            var config = new MiddleWare_Config()
            {
                RequestMethod = HttpMethod.Post,
                RequestUrl    = setup.SecureURL_fbm,
                Token         = setup.AuthToken
            };
            var caller = new APICaller(config);

            return(caller.CallApi(metadataShipemt));
        }
예제 #3
0
        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());
        }