public List <DatColStockModel> MapToDatCol(Int32 vendorId, List <Model.WehkampStockMutation> mutations)
        {
            using (var pDb = new PetaPoco.Database(Environments.Current.Connection, "System.Data.SqlClient"))
            {
                int connectorID = pDb.FirstOrDefault <int>(@"select connectorid from contentproduct where vendorid = @0 and isassortment = 1", vendorId);

                int    shopNumber           = ConnectorHelper.GetStoreNumber(connectorID);
                int    differenteShopNumber = VendorHelper.GetDifferenceShopNumber(vendorId);
                string employeeNumber       = VendorHelper.GetEmployeeNumber(vendorId);
                var    salesSlipNumber      = ReceiptHelper.GetSlipNumber(vendorId);

                int _receiptIndex       = GenericSlipNumberHelper.GetSlipNumberForTransfer(vendorId, ReceiptHelper.STOCK_SALESSLIP_RECEIPT_NUMBER_SETTING_KEY);
                int _receiptIndexSurplu = GenericSlipNumberHelper.GetSlipNumberForTransfer(vendorId, ReceiptHelper.STOCK_SALESSLIP_RECEIPT_NUMBER_SETTING_KEY_SURPLUS);
                mutations.ForEach(mutation =>
                {
                    _index += 200;

                    var line = new DatColStockModel
                    {
                        StoreNumber                    = (mutation.MutationQuantity > 0 ? differenteShopNumber : shopNumber).ToString("D3") + " 01",
                        EmployeeNumber                 = employeeNumber,
                        ReceiptNumber                  = salesSlipNumber,
                        TransactionType                = "20",
                        DateNotified                   = mutation.MutationDate.ToString("yyyyMMddHHmm"),
                        RecordType                     = "01",
                        SubType                        = "00",
                        NumberOfSkus                   = Math.Abs(mutation.MutationQuantity),
                        MancoOrSurplus                 = mutation.MutationQuantity > 0 ? shopNumber : differenteShopNumber,
                        FixedField1                    = "000000000+",
                        RecordSequence                 = _index,
                        FixedField2                    = "000",
                        FixedField3                    = "000000000+",
                        FixedField4                    = "000",
                        FixedField5                    = "000000000+",
                        FixedField6                    = "000",
                        OriginalSellingPrice           = (int)Math.Round(PriceHelper.GetPrice(mutation.ProductID, vendorId) * 100),
                        FixedField7                    = "00",
                        ArticleNumberColorCodeSizeCode = ProductHelper.GetPFAItemNumber(mutation.Articlenumber, mutation.Colorcode, mutation.ProductID),
                        Barcode                        = BarcodeHelper.GetBarcode(mutation.ProductID),
                        Receipt                        = string.Format("{0}{1}{2}", 0, mutation.MutationQuantity > 0 ? differenteShopNumber.ToString("D3") : shopNumber.ToString(), _receiptIndex.ToString().PadLeft(4, '0')),
                        TaxCode                        = "1",
                        EmployeeNumber2                = employeeNumber,
                        ScannedWithBarcodeReader       = 0
                    };

                    _list.Add(line);
                });
                ReceiptHelper.IncrementSalesSlipNumber(ref salesSlipNumber, vendorId, ReceiptHelper.STOCK_SALESSLIP_NUMBER_SETTINGKEY);

                return(_list);
            }
        }
        protected override void Process()
        {
            using (var unit = GetUnitOfWork())
            {
                var vendors = unit.Scope.Repository <Vendor>().GetAll().ToList().Where(c => ((VendorType)c.VendorType).Has(VendorType.SupportsPFATransferOrders)).ToList();

                var orderLedgerRepo = unit.Scope.Repository <OrderLedger>();

                foreach (var vendor in vendors)
                {
                    var rule = vendor.ContentProducts.FirstOrDefault(c => c.IsAssortment);

                    rule.ThrowIfNull("Publication rule with IsAssortment is missing for vendor " + vendor.Name);

                    IQueryable <OrderLine> lines = unit.Scope.Repository <OrderLine>().GetAll(x => x.Order.OrderType == (int)OrderTypes.ReturnOrder && x.Order.ConnectorID == rule.ConnectorID && x.OrderLedgers.Any(y => y.Status == (int)OrderLineStatus.StockReturnRequestConfirmation));

                    IQueryable <OrderLine> processed = unit.Scope.Repository <OrderLine>().GetAll(x => x.Order.OrderType == (int)OrderTypes.ReturnOrder && x.Order.ConnectorID == rule.ConnectorID && x.OrderLedgers.Any(y => y.Status == (int)OrderLineStatus.ProcessedStockReturnRequestConfirmation));

                    var ledgerRepo = unit.Scope.Repository <OrderLedger>();

                    var storeID              = ConnectorHelper.GetStoreNumber(rule.ConnectorID);
                    var employeeNumber       = VendorHelper.GetEmployeeNumber(vendor.VendorID);
                    int differenceShopNumber = VendorHelper.GetReturnDifferenceShopNumber(vendor.VendorID);

                    lines = lines.Except(processed);

                    var toProcess = lines.GroupBy(line => line.Order).ToList().Select(c => new
                    {
                        Order      = c.Key,
                        OrderLines = c.ToList()
                    }).ToList();

                    foreach (var order in toProcess)
                    {
                        int recordSequenceDetail          = 0;
                        int recordSequeceCompleteShipment = 200;

                        //get all three different shop numbers
                        var salesSlipNumber = GenericSlipNumberHelper.GetSlipNumberForTransfer(vendor.VendorID, ReceiptHelper.STOCK_SALESSLIP_RECEIPT_NUMBER_SETTING_KEY);
                        int salesSlipNumber_OtherFilials = 0; //used for any line with warehouse code != 982


                        var salesSlipNumberComplete = salesSlipNumber;
                        var salesSlipNumberShop     = salesSlipNumberComplete;

                        var fileEngine = new FileHelperEngine(typeof(ReturnOrderDatcolModel));
                        var returns    = new List <ReturnOrderDatcolModel>();

                        var fileName = String.Format("{0}{1}", "returnOrders", DateTime.Now.ToString("yyyyMMddHHmmss"));

                        //process contents
                        DateTime messageTime = order.OrderLines.FirstOrDefault().OrderLedgers.FirstOrDefault(c => c.Status == (int)OrderLineStatus.StockReturnRequestConfirmation).LedgerDate.ToLocalTime();
                        int      totalSkus   = 0;

                        if (order.OrderLines.Any(c => c.WareHouseCode != differenceShopNumber.ToString()))
                        {
                            salesSlipNumber_OtherFilials = GenericSlipNumberHelper.GetSlipNumberForTransfer(vendor.VendorID, ReceiptHelper.STOCK_SALESSLIP_RECEIPT_NUMBER_SETTING_KEY);
                        }

                        foreach (var orderLine in order.OrderLines)
                        {
                            var detailSalesSlipNumber = salesSlipNumber;

                            var ledger = orderLine.OrderLedgers.FirstOrDefault(c => c.Status == (int)OrderLineStatus.StockReturnRequestConfirmation);
                            totalSkus += ledger.Quantity.Value;

                            recordSequenceDetail += 200;

                            if (orderLine.WareHouseCode != differenceShopNumber.ToString())
                            {
                                returns.Add(GetDetailLevel(storeID, employeeNumber, salesSlipNumber, salesSlipNumber_OtherFilials, ledger.LedgerDate.ToLocalTime(), ledger.Quantity.Value, differenceShopNumber, recordSequenceDetail, orderLine.ProductID.Value, vendor.VendorID));
                            }

                            else
                            {
                                returns.Add(GetDetailLevel(storeID, employeeNumber, salesSlipNumber, salesSlipNumber, ledger.LedgerDate.ToLocalTime(), ledger.Quantity.Value, differenceShopNumber, recordSequenceDetail, orderLine.ProductID.Value, vendor.VendorID));
                            }
                        }

                        var codes = order.OrderLines.Select(c => c.WareHouseCode).Where(c => !string.IsNullOrEmpty(c));

                        if (codes.Any(c => c != differenceShopNumber.ToString()))
                        {
                            returns.Add(GetCompleteLevel(storeID, employeeNumber, salesSlipNumberComplete, salesSlipNumber_OtherFilials, messageTime, totalSkus, differenceShopNumber));
                        }

                        foreach (var orderLineFilial in order.OrderLines.Where(c => !string.IsNullOrEmpty(c.WareHouseCode) && c.WareHouseCode != differenceShopNumber.ToString()).GroupBy(c => c.WareHouseCode).ToList())
                        {
                            var salesSlipNumber_Difference = GenericSlipNumberHelper.GetSlipNumberForTransfer(vendor.VendorID, ReceiptHelper.STOCK_SALESSLIP_RECEIPT_NUMBER_SETTING_KEY_SURPLUS);
                            foreach (var orderLine in orderLineFilial)
                            {
                                recordSequenceDetail += 200;
                                var ledger = orderLine.OrderLedgers.FirstOrDefault(c => c.Status == (int)OrderLineStatus.StockReturnRequestConfirmation);

                                returns.Add(GetDetailLevelForStore(int.Parse(orderLine.WareHouseCode), employeeNumber, salesSlipNumberShop, salesSlipNumber_Difference, messageTime, ledger.Quantity.Value, differenceShopNumber, recordSequenceDetail, orderLine.ProductID.Value, vendor.VendorID));
                            }
                        }

                        if (returns.Count > 0)
                        {
                            var file = fileEngine.WriteString(returns);
                            var path = CommunicatorService.GetMessagePath(vendor.VendorID, PfaCommunicator.Objects.Models.MessageTypes.WehkampReturn);
                            File.WriteAllText(Path.Combine(path.MessagePath, fileName), file);
                        }

                        foreach (var line in order.OrderLines)
                        {
                            line.SetStatus(OrderLineStatus.ProcessedStockReturnRequestConfirmation, ledgerRepo);
                        }
                    }
                    unit.Save();
                }
            }
        }
Esempio n. 3
0
        protected override void Process()
        {
            using (var unit = GetUnitOfWork())
            {
                var vendors           = unit.Scope.Repository <Vendor>().GetAll().ToList().Where(c => ((VendorType)c.VendorType).Has(VendorType.SupportsPFATransferOrders)).ToList();
                int transferOrderType = (int)OrderTypes.TransferOrder;
                var ledgerRepo        = unit.Scope.Repository <OrderLedger>();


                vendors.ForEach((vendor, indexer) =>
                {
                    var mutationEngine = new FileHelperEngine(typeof(ReceivedTransferMutation));

                    var fileName = String.Format("{0}{1}", "mancosurplus", DateTime.Now.ToString("yyyyMMddHHmmss"));

                    var mutations = new List <ReceivedTransferMutation>();

                    var rule = vendor.ContentProducts.FirstOrDefault(c => c.IsAssortment);

                    rule.ThrowIfNull("Publication rule with IsAssortment is missing for vendor " + vendor.Name);

                    List <OrderLine> lines = unit
                                             .Scope
                                             .Repository <OrderLine>()
                                             .GetAll(x => x.Order.OrderType == transferOrderType &&
                                                     x.Order.ConnectorID == rule.ConnectorID &&
                                                     x.OrderLedgers.Any(y => y.Status == (int)OrderLineStatus.ReceivedTransfer) &&
                                                     !x.OrderLedgers.Any(y => y.Status == (int)OrderLineStatus.ProcessedReceivedTransfer)).ToList();

                    var storeID        = ConnectorHelper.GetStoreNumber(rule.ConnectorID);
                    var employeeNumber = VendorHelper.GetEmployeeNumber(vendor.VendorID);

                    var salesSlipNumber = ReceiptHelper.GetSlipNumber(vendor.VendorID);
                    //ReceiptHelper.IncrementSalesSlipNumber(ref salesSlipNumber);

                    var toProcess = lines.GroupBy(k => k.Order).ToList().Select(c => new
                    {
                        Order           = c.Key,
                        OrderLines      = c.ToList(),
                        ExtraOrderLines = c.Key.OrderLines.Except(c.ToList())
                    }).ToList();

                    foreach (var order in toProcess)
                    {
                        PfaOrderHelper helper = new PfaOrderHelper(vendor.VendorID);

                        if (order.OrderLines.Count == 0)
                        {
                            continue;
                        }

                        order.OrderLines.AddRange(order.ExtraOrderLines);

                        var receivedByWehkampDate = order.OrderLines.FirstOrDefault().OrderLedgers.FirstOrDefault(c => c.Status == (int)OrderLineStatus.ReceivedTransfer).LedgerDate.ToLocalTime();

                        List <TransferOrderModel> list = helper.GetShippedQuantitiesForOrder(order.Order.WebSiteOrderNumber, order.Order.ConnectorID);
                        if (list.Count > 0)
                        {
                            List <TransferOrderLine> orderLinesToBeProcessed = GetOrderLineStatus(order.OrderLines, list, order.Order.OrderID, vendor.VendorID, unit);

                            if (list.Count > order.OrderLines.Count())
                            {
                                //more lines are in PFA than in the Concentrator
                                log.AuditInfo(string.Format("For order {0} there are {1} order lines in Concentrator and {2} in PFA. Difference found of {3} lines."
                                                            , order.Order.WebSiteOrderNumber, order.OrderLines.Count(), list.Count, order.ExtraOrderLines.Count()));
                            }

                            if (orderLinesToBeProcessed.Any(c => c.ShippedFromPFA != c.ReceivedFromWehkamp))
                            {
                                var differences = orderLinesToBeProcessed.Where(c => c.ShippedFromPFA != c.ReceivedFromWehkamp);

                                //add each quantity unmatched product
                                differences.ForEach((difference, index) =>
                                {
                                    var unmatchedQuantityLine = GetDifferenceRuleForIncompleteShipping(difference, orderLinesToBeProcessed.Sum(x => x.ReceivedFromWehkamp), orderLinesToBeProcessed.Sum(x => x.ShippedFromPFA), storeID, employeeNumber, vendor.VendorID, receivedByWehkampDate, salesSlipNumber);

                                    mutations.Add(unmatchedQuantityLine);
                                });

                                //add the total rule
                                mutations.AddRange(GetTotalRuleForIncompleteShipping(order.Order, orderLinesToBeProcessed.Sum(x => x.ReceivedFromWehkamp), orderLinesToBeProcessed.Sum(x => x.ShippedFromPFA), storeID, employeeNumber, vendor.VendorID, receivedByWehkampDate, salesSlipNumber));
                            }
                            else
                            {
                                //add quantity matched list
                                mutations.AddRange(GetCompletelyShippedRule(order.Order, orderLinesToBeProcessed.Sum(x => x.ReceivedFromWehkamp), storeID, employeeNumber, vendor.VendorID, receivedByWehkampDate, salesSlipNumber));
                            }
                        }
                        else
                        {
                            log.AuditError("Ignoring order " + order.Order.WebSiteOrderNumber + ": No lines found in PFA");
                        }
                        foreach (var line in order.OrderLines)
                        {
                            line.SetStatus(OrderLineStatus.ProcessedReceivedTransfer, ledgerRepo);
                        }
                        ReceiptHelper.IncrementSalesSlipNumber(ref salesSlipNumber, vendor.VendorID);
                    }

                    if (mutations.Count > 0)
                    {
                        var file = mutationEngine.WriteString(mutations);

                        var path = CommunicatorService.GetMessagePath(vendor.VendorID, MessageTypes.TransferOrderConfirmation);

                        File.WriteAllText(Path.Combine(path.MessagePath, fileName), file);
                    }
                    unit.Save();
                });
            }
        }