Esempio n. 1
0
        //TODO Invalid file exception
        public SalesDocument LoadDocument()
        {
            int     docId       = 0;
            decimal checkTotals = 0;

            if (doc != null)
            {
                return(doc);
            }
            string context = fileInfo.ReadAllText();

            string[] lines = Str.Split(context, "\r\n", true);
            foreach (String line in lines)
            {
                try
                {
                    switch (line.Substring(8, 2))
                    {
                    case "01":    //FIS
                    case "02":    //FAT,IAD,IRS
                        switch (line.Substring(11, 3))
                        {
                        case PosMessage.HR_CODE_INVOICE:
                            doc = new Invoice();
                            break;

                        case PosMessage.HR_CODE_RETURN:
                            doc = new ReturnDocument();
                            break;

                        case PosMessage.HR_CODE_WAYBILL:
                            doc = new Waybill();
                            break;

                        default:
                            doc = new Receipt();
                            break;
                        }
                        docId           = int.Parse(line.Substring(28, 6));
                        doc.SalesPerson = cr.DataConnector.FindCashierById(line.Substring(23, 4));
                        break;

                    case "03":    //TAR
                        //1,00011,03,TAR,25/03/2009  ,00:21:26
                        DateTime date = DateTime.Now;
                        Parser.TryDate(line.Substring(15, 10).Replace('/', '.') + line.Substring(27, 9), out date);
                        break;

                    case "04":    //SAT
                        FiscalItem si = new SalesItem();
                        si.Quantity    = Convert.ToDecimal(line.Substring(15, 6)) / 1000;
                        si.Product     = cr.DataConnector.FindProductByLabel(line.Substring(21, 6));
                        si.TotalAmount = Convert.ToDecimal(line.Substring(30, 10)) / 100m;
                        checkTotals   += si.TotalAmount;
                        si.UnitPrice   = Math.Round(si.TotalAmount / si.Quantity, 2);

                        if (si.Quantity == 99)
                        {
                            if (tempSalesItem == null)
                            {
                                tempSalesItem         = new SalesItem();
                                tempSalesItem.Product = si.Product;
                            }

                            if (tempSalesItem.Quantity >= 99)
                            {
                                if (tempSalesItem.Product == si.Product)
                                {
                                    tempSalesItem.Quantity += 99;
                                }
                                else
                                {
                                    doc.AddItem(tempSalesItem, false);
                                    tempSalesItem = new SalesItem();
                                    tempSalesItem = (SalesItem)si;
                                }
                            }
                            else
                            {
                                tempSalesItem.Quantity += 99;
                                tempSalesItem.Quantity -= 1;     // constructor's default quantity.
                            }
                        }
                        else
                        {
                            if (tempSalesItem == null)
                            {
                                tempSalesItem = (SalesItem)si;
                            }

                            if (tempSalesItem.Quantity >= 99)
                            {
                                if (tempSalesItem.Product == si.Product)
                                {
                                    tempSalesItem.Quantity += si.Quantity;
                                    doc.AddItem(tempSalesItem, false);
                                    tempSalesItem = null;
                                }
                                else
                                {
                                    doc.AddItem(tempSalesItem, false);
                                    tempSalesItem = null;
                                    doc.AddItem(si, false);
                                }
                            }
                            else
                            {
                                doc.AddItem(si, false);
                                //tempSalesItem = null;
                            }
                        }
                        break;

                    case "05":    //IPT
                        if (tempSalesItem != null && tempSalesItem.Quantity >= 99)
                        {
                            doc.AddItem(tempSalesItem, false);
                        }

                        FiscalItem item = new SalesItem();
                        item.Quantity    = Convert.ToDecimal(line.Substring(15, 6)) / 1000;
                        item.Product     = cr.DataConnector.FindProductByLabel(line.Substring(21, 6));
                        item.TotalAmount = Convert.ToDecimal(line.Substring(30, 10)) / 100m;
                        checkTotals     -= item.TotalAmount;
                        FiscalItem vi = new VoidItem(item);
                        vi.UnitPrice = Math.Round(vi.TotalAmount / vi.Quantity, 2);
                        doc.AddItem(vi, false);

                        decimal vq = Math.Abs(vi.Quantity);
                        decimal va = Math.Abs(vi.TotalAmount);
                        for (int i = 0; i < doc.Items.Count; i++)
                        {
                            if (!(doc.Items[i] is SalesItem))
                            {
                                continue;
                            }

                            if ((doc.Items[i].Product.Id == vi.Product.Id) &&
                                (doc.Items[i].UnitPrice == vi.UnitPrice))
                            {
                                if (vq > doc.Items[i].Quantity)
                                {
                                    ((SalesItem)doc.Items[i]).VoidQuantity = doc.Items[i].Quantity;
                                    ((SalesItem)doc.Items[i]).VoidAmount   = doc.Items[i].TotalAmount;
                                    vq -= doc.Items[i].Quantity;
                                    va -= doc.Items[i].TotalAmount;
                                }
                                else
                                {
                                    if (((SalesItem)doc.Items[i]).VoidQuantity + vq >
                                        (((SalesItem)doc.Items[i]).Quantity - ((SalesItem)doc.Items[i]).VoidQuantity))
                                    {
                                        continue;
                                    }
                                    ((SalesItem)doc.Items[i]).VoidQuantity += vq;
                                    ((SalesItem)doc.Items[i]).VoidAmount   += va;
                                    break;
                                }
                            }
                        }
                        break;

                    case "06":    //IND,ART
                        AdjustmentType adjustmentType = AdjustmentType.Discount;
                        int            percentage     = 0;
                        bool           isPercent      = Parser.TryInt(line.Substring(25, 2), out percentage);

                        if (tempSalesItem.Quantity >= 99)
                        {
                            doc.AddItem(tempSalesItem, false);
                            tempSalesItem = null;
                        }
                        IAdjustable target = doc.LastItem;
                        if (line.Substring(15, 3) == "TOP")
                        {
                            target = doc;
                        }

                        if (isPercent)
                        {
                            adjustmentType = AdjustmentType.PercentDiscount;
                        }

                        if (line.Substring(11, 3) == "ART")
                        {
                            adjustmentType = isPercent ? AdjustmentType.PercentFee : AdjustmentType.Fee;
                        }

                        decimal amount = Convert.ToDecimal(line.Substring(30, 10)) / 100m;

                        if (adjustmentType == AdjustmentType.PercentFee || adjustmentType == AdjustmentType.Fee)
                        {
                            checkTotals += amount;
                        }
                        else
                        {
                            checkTotals -= amount;
                        }
                        if (isPercent)
                        {
                            amount = (decimal)percentage;
                        }
                        Adjustment adj = new Adjustment(target, adjustmentType, amount);
                        if (Math.Abs(adj.NetAmount) != Convert.ToDecimal(line.Substring(30, 10)) / 100m)
                        {
                            bool minus = adj.NetAmount < 0 ? true : false;
                            adj.NetAmount = Convert.ToDecimal(line.Substring(30, 10)) / 100m;
                            if (minus)
                            {
                                adj.NetAmount *= -1;
                            }
                        }
                        adj.AuthorizingCashierId = line.Substring(19, 4);

                        //Applying subtotal adjustments to retrieved documents
                        //could have side-effects. Only do item adjustments
                        // if (target is FiscalItem)
                        target.Adjust(adj);
                        break;

                    case "08":
                        if (tempSalesItem != null && tempSalesItem.Quantity >= 99)
                        {
                            doc.AddItem(tempSalesItem, false);
                        }
                        doc.TotalAmount = Convert.ToDecimal(line.Substring(30, 10)) / 100m;
                        int statusCode = 0;
                        if (Parser.TryInt(line.Substring(26, 1), out statusCode))
                        {
                            status = (DocumentStatus)statusCode;
                        }

                        break;

                    case "09":
                        amount = Convert.ToDecimal(line.Substring(30, 10)) / 100m;
                        switch (line.Substring(11, 3))
                        {
                        case "NAK":
                            doc.Payments.Add(new CashPaymentInfo(amount));
                            break;

                        case "CEK":
                            doc.Payments.Add(new CheckPaymentInfo(amount, line.Substring(15, 12)));
                            break;

                        case "DVZ":
                            int currId = 0;
                            if (Parser.TryInt(line.Substring(15, 1), out currId))
                            {
                                ICurrency c = cr.DataConnector.GetCurrencies()[(int)(currId + "")[0]];
                                doc.Payments.Add(new CurrencyPaymentInfo(c, amount));
                            }
                            break;
                        }
                        break;

                    case "10":    //KRD
                        amount = Convert.ToDecimal(line.Substring(30, 10)) / 100m;
                        int creditId    = 0;
                        int installment = 0;
                        if (Parser.TryInt(line.Substring(25, 2), out creditId))
                        {
                            ICredit c = cr.DataConnector.GetCredits()[creditId];
                            if (c != null)
                            {
                                Parser.TryInt(line.Substring(15, 2), out installment);
                                doc.Payments.Add(new CreditPaymentInfo(c, amount, installment));
                            }
                        }
                        break;

                    case "11":    //SON
                        decimal paidTotal = 0.00m;
                        foreach (PaymentInfo pi in doc.Payments)
                        {
                            paidTotal += pi.Amount;
                        }

                        //check if there are invalid values on file
                        if (checkTotals != doc.TotalAmount)
                        {
                            throw new Exception();
                        }

                        if (paidTotal < doc.TotalAmount)
                        {
                            throw new Exception();
                        }

                        if (paidTotal >= doc.TotalAmount)
                        {
                            status = DocumentStatus.Closed;
                        }
                        break;

                    case "29":    //NOT
                        doc.FootNote.Add(line.Substring(15, 12) + line.Substring(28, 12));
                        break;

                    case "36":    //SNO
                        string serialNo = line.Substring(15, 12) + line.Substring(28);
                        doc.LastItem.SerialNo = serialNo;
                        break;

                    case "37":    //PNO
                        string batchNo = line.Substring(15, 12) + line.Substring(28);
                        doc.LastItem.BatchNumber = batchNo;
                        break;

                    case "39":     //SKT
                        date = DateTime.Now;
                        Parser.TryDate(line.Substring(15, 10).Replace('/', '.') + line.Substring(27, 9), out date);
                        doc.LastItem.ExpiryDate = date;
                        break;

                    default: break;
                    }
                }
                catch (Exception err)
                {
                    cr.Log.Warning("siparis dosyasý hata : \n\t satýr: " + line);
                    VoidOrder();
                    throw err;
                }
            }
            if (id == 0)
            {
                id = docId;
            }
            doc.Id                    = id;
            doc.SalesPerson           = salesPerson;
            doc.ResumedFromDocumentId = resumedFromDocumentId;
            this.creationTime         = fileInfo.CreationTime;
            return(doc);
        }
Esempio n. 2
0
        private TCPMsgType ProcessOrder(String orderLines, Socket client)
        {
            String [] lines = orderLines.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            byte []   buff  = new byte[1];

            SalesDocument doc = null;

            TCPMsgType    retVal      = TCPMsgType.TCP_MSG_SALE;
            List <string> reportLines = new List <string>();

            CashRegister.Log.Info(orderLines);

            foreach (String orderLine in lines)
            {
                try
                {
                    switch (orderLine.Substring(8, 2))
                    {
                    case "01":    //FIS
                    case "02":    //FAT,IAD,IRS
                        CheckCashier();
                        switch (orderLine.Substring(11, 3))
                        {
                        case PosMessage.HR_CODE_INVOICE:
                            doc = new Invoice(); break;

                        case PosMessage.HR_CODE_RETURN:
                            doc = new ReturnDocument(); break;

                        case PosMessage.HR_CODE_WAYBILL:
                            doc = new Waybill(); break;

                        default:
                            doc = new Receipt();
                            break;
                        }

                        if (doc.DocumentTypeId >= 0)
                        {
                            doc.SlipSerialNo = orderLine.Substring(17, 10);
                            doc.TotalAmount  = Convert.ToDecimal(orderLine.Substring(30, 10)) / 100m;
                        }
                        CashRegister.Printer.AdjustPrinter(doc);
                        CashRegister.Printer.PrintHeader(doc);
                        //doc.SalesPerson = cr.DataConnector.FindCashierById(line.Substring(23, 4));
                        break;

                    case "03":    //TAR
                        //1,00011,03,TAR,25/03/2009  ,00:21:26
                        break;

                    case "04":    //SAT
                    case "05":
                        CheckCashier();
                        if (!(doc is Receipt))
                        {
                            break;
                        }
                        FiscalItem si = new SalesItem();

                        String   pluNo     = orderLine.Substring(21, 6);
                        decimal  amount    = Convert.ToDecimal(orderLine.Substring(30, 10)) / 100m;
                        decimal  quantity  = Convert.ToDecimal(orderLine.Substring(15, 6)) / 1000m;
                        int      depID     = Convert.ToInt32(orderLine.Substring(28, 2));
                        String   name      = orderLine.Substring(41, 20).Trim().ToUpper();
                        decimal  unitPrice = Math.Round(amount / quantity, 2);
                        String   unit      = orderLine.Substring(62, 4).Trim().ToUpper();
                        IProduct prd       = Data.Connector.Instance().CreateProduct(name + "DYNAMIC", Department.Departments[depID], unitPrice);

                        si.Quantity    = quantity;
                        si.Product     = prd;
                        si.TotalAmount = amount;
                        si.UnitPrice   = unitPrice;
                        si.Unit        = unit;
                        if (orderLine.Contains("SAT"))
                        {
                            CashRegister.Printer.Print(si);
                            doc.Items.Add(si);
                            doc.TotalAmount += si.TotalAmount;
                            if (doc.ProductTotals.ContainsKey(si.Product))
                            {
                                doc.ProductTotals[si.Product] += si.TotalAmount;
                            }
                            else
                            {
                                doc.ProductTotals.Add(si.Product, si.TotalAmount);
                            }
                        }
                        else
                        {
                            VoidItem vi = new VoidItem(si);
                            CashRegister.Printer.Print(vi);
                            doc.Items.Add(vi);
                            doc.TotalAmount -= vi.TotalAmount;
                            if (doc.ProductTotals.ContainsKey(vi.Product))
                            {
                                doc.ProductTotals[vi.Product] -= vi.TotalAmount;
                            }
                        }
                        break;

                    case "06":    //IND,ART
                        CheckCashier();
                        AdjustmentType adjustmentType = AdjustmentType.Discount;
                        int            percentage     = 0;
                        bool           isPercent      = Parser.TryInt(orderLine.Substring(25, 2), out percentage);

                        IAdjustable target = doc.Items[doc.Items.Count - 1];
                        if (orderLine.Substring(15, 3) == "TOP")
                        {
                            target = doc;
                            decimal total = CashRegister.Printer.PrinterSubTotal;
                            CashRegister.Printer.PrintSubTotal(doc, true);
                        }

                        if (isPercent)
                        {
                            adjustmentType = AdjustmentType.PercentDiscount;
                        }

                        if (orderLine.Substring(11, 3) == "ART")
                        {
                            adjustmentType = isPercent ? AdjustmentType.PercentFee : AdjustmentType.Fee;
                        }

                        decimal adjAmount = Convert.ToDecimal(orderLine.Substring(30, 10)) / 100m;
                        if (isPercent)
                        {
                            adjAmount = (decimal)percentage;
                        }
                        Adjustment adj = new Adjustment(target, adjustmentType, adjAmount);
                        CashRegister.Printer.Print(adj);

                        target.Adjust(adj);

                        if (adj.Target is SalesItem)
                        {
                            SalesItem adjTarget = (SalesItem)adj.Target;
                            if (doc.ProductTotals.ContainsKey(adjTarget.Product))
                            {
                                doc.ProductTotals[adjTarget.Product] += adj.NetAmount;
                            }
                            doc.TotalAmount += adj.NetAmount;
                        }
                        break;

                    case "08":
                        CheckCashier();
                        decimal totalAmount = Convert.ToDecimal(orderLine.Substring(30, 10)) / 100m;
                        //todo: compare total amaounts
                        //DocumentStatus status = (DocumentStatus)Convert.ToInt16(orderLine.Substring(26, 1));
                        decimal subTotal = CashRegister.Printer.PrinterSubTotal;
                        CashRegister.Printer.PrintTotals(doc, true);
                        break;

                    case "09":
                        CheckCashier();
                        decimal payment = Convert.ToDecimal(orderLine.Substring(30, 10)) / 100m;
                        switch (orderLine.Substring(11, 3))
                        {
                        case "NAK":
                            CashRegister.Printer.Pay(payment);
                            doc.Payments.Add(new CashPaymentInfo(payment));
                            break;

                        case "CEK":
                            String refNum = orderLine.Substring(15, 12).Trim();
                            CashRegister.Printer.Pay(payment, refNum);
                            doc.Payments.Add(new CheckPaymentInfo(payment, refNum));
                            break;

                        case "DVZ":
                            String  exCode = orderLine.Substring(15, 1);
                            decimal exAmnt = Convert.ToDecimal(orderLine.Substring(18, 9)) / 100m;
                            String  exName = orderLine.Substring(41, 20);
                            int     currId = int.Parse(orderLine.Substring(62, 2));

                            ICurrency currency = Data.Connector.Instance().GetCurrencies()[currId];

                            CashRegister.Printer.Pay(payment, currency);
                            doc.Payments.Add(new CurrencyPaymentInfo(currency, payment));
                            break;
                        }
                        break;

                    case "10":
                        CheckCashier();
                        decimal crdPayment   = Convert.ToDecimal(orderLine.Substring(30, 10)) / 100m;
                        int     installments = int.Parse(orderLine.Substring(15, 2));
                        int     crdId        = int.Parse(orderLine.Substring(25, 2));
                        String  crdName      = orderLine.Substring(41, 20);

                        ICredit crd = Data.Connector.Instance().GetCredits()[crdId];

                        CashRegister.Printer.Pay(crdPayment, crd, installments);
                        doc.Payments.Add(new CreditPaymentInfo(crd, crdPayment));
                        break;

                    case "11":
                        CheckCashier();
                        CashRegister.Printer.PrintFooter(doc);
                        Data.Connector.Instance().OnDocumentClosed(doc);
                        break;

                    case "22":
                        CheckCashier();
                        String remark = orderLine.Substring(15, orderLine.Length - 15).TrimEnd();
                        CashRegister.Printer.PrintRemark(remark);
                        break;

                    case "12":    //cash out
                        CheckCashier();
                        decimal cashOut = Convert.ToDecimal(orderLine.Substring(30, 10)) / 100m;
                        CashRegister.Printer.Withdraw(cashOut);
                        CashRegister.DataConnector.OnWithdrawal(cashOut);
                        retVal = TCPMsgType.TCP_MSG_CASH_OUT;
                        break;

                    case "13":    //cash in
                        CheckCashier();
                        decimal cashIn = Convert.ToDecimal(orderLine.Substring(30, 10)) / 100m;
                        CashRegister.Printer.Deposit(cashIn);
                        CashRegister.DataConnector.OnDeposit(cashIn);
                        retVal = TCPMsgType.TCP_MSG_CASH_IN;
                        break;

                    case "14":    //log in
                        if (orderLine.Substring(11, 3) != "LIN")
                        {
                            retVal = TCPMsgType.TCP_MSG_LOG_IN;
                            break;
                        }
                        String   cashierID   = orderLine.Substring(23, 4);
                        String   cashierPass = orderLine.Substring(28, 8);
                        String   cashierName = String.Format("KASIYER {0}", int.Parse(cashierID)); //orderLine.Substring(41, 20).Trim().ToUpper();
                        ICashier ch;
                        ch          = CashRegister.DataConnector.CreateCashier(cashierName + "FPU", cashierID);
                        ch.Password = cashierPass.Trim(new char[] { ' ', ',' });

                        /* ************ TODO: REMOVED ***************/
                        if (ch.Password == String.Empty)
                        {
                            switch (cashierID)
                            {
                            case "0001":
                                ch.Password = "******";
                                break;

                            case "0006":
                                ch.Password = "******";
                                break;

                            case "0009":
                                ch.Password = "******";
                                break;
                            }
                        }
                        /* ************ END TEMPRORARY CODE ***********/

                        try
                        {
                            CashRegister.Printer.SignInCashier(int.Parse(cashierID), ch.Password);
                            CashRegister.CurrentCashier = ch;
                        }
                        catch (CashierAlreadyAssignedException cEx)
                        {
                            if (cEx.CashierId == ch.Id)
                            {
                                CashRegister.CurrentCashier = ch;
                            }
                        }
                        catch
                        {
                        }
                        retVal = TCPMsgType.TCP_MSG_LOG_IN;
                        break;

                    case "15":    //log out
                        //String chID = orderLine.Substring(26, 4);
                        //String chName = orderLine.Substring(41, 20).Trim().ToUpper();
                        //ICashier chOut;
                        //chOut = CashRegister.DataConnector.CreateCashier(chName + "DYNAMIC", chID);
                        try
                        {
                            if (CashRegister.CurrentCashier != null)
                            {
                                CashRegister.Printer.SignOutCashier();
                            }
                            CashRegister.CurrentCashier = null;
                        }
                        catch
                        {
                        }
                        retVal = TCPMsgType.TCP_MSG_LOG_OUT;
                        break;

                    case "16":    //Daily report
                        CheckCashier();
                        switch (orderLine.Substring(11, 3))
                        {
                        case "ZRP":
                            CashRegister.Printer.PrintZReport();
                            break;

                        case "XRP":
                            CashRegister.Printer.PrintXReport(true);
                            break;
                        }
                        retVal = TCPMsgType.TCP_MSG_DAILY_REPORT;
                        break;

                    case "81":    //reports form
                        CheckCashier();
                        retVal = TCPMsgType.TCP_MSG_REPORT;
                        break;

                    case "82":    //paper cut
                        CheckCashier();
                        CashRegister.Printer.CutPaper();
                        retVal = TCPMsgType.TCP_MSG_CUT_PAPER;
                        break;

                    case "83":    //custom report
                        CheckCashier();
                        int nfDocType = 1;
                        switch (orderLine.Substring(11, 3))
                        {
                        case "ORP":        //stands for "OZEL RAPOR"
                            reportLines.Clear();
                            nfDocType = int.Parse(orderLine.Substring(15, 1));
                            break;

                        case "RSA":        //stands for "RAPOR SATIRI"
                            String repLine = orderLine.Substring(15).ToUpper();
                            reportLines.Add(repLine);
                            break;

                        case "RSO":        //stands for "RAPOR SONU"
                            if (nfDocType == 1)
                            {
                                CashRegister.Printer.PrintCustomReport(reportLines.ToArray());
                            }
                            else if (nfDocType == 2)
                            {
                                CashRegister.Printer.PrintCustomReceipt(reportLines.ToArray());
                            }
                            break;
                        }

                        retVal = TCPMsgType.TCP_MSG_CUSTOM_REPORT;
                        break;

                    case "84":    //date time setting
                        //1,00011,03,TAR,25/03/2009  ,00:21:26
                        int day  = int.Parse(orderLine.Substring(15, 2));
                        int mon  = int.Parse(orderLine.Substring(18, 2));
                        int year = int.Parse(orderLine.Substring(21, 4));

                        int hour = int.Parse(orderLine.Substring(28, 2));
                        int min  = int.Parse(orderLine.Substring(31, 2));
                        int sec  = 0;
                        try
                        {
                            sec = int.Parse(orderLine.Substring(34, 2));
                        }
                        catch { }

                        DateTime dtDate = new DateTime(year, mon, day, hour, min, sec);
                        CashRegister.Printer.Time = dtDate;
                        break;

                    case "85":    //Payment order
                        CheckCashier();
                        switch (orderLine.Substring(11, 3))
                        {
                        case "ABA":        //stands for "ADISYON BASLANGICI"
                            ISalesDocument invoice = new Invoice();
                            CashRegister.Printer.AdjustPrinter(invoice);
                            CashRegister.Printer.StartSlip(invoice);
                            break;

                        case "ASA":        //stands for "ADİSYON SATIRI"
                            String repLine = orderLine.Substring(15).ToUpper();
                            CashRegister.Printer.PrintSlipLine(repLine);
                            break;

                        case "ASO":        //stands for "ADİSYON SONU"
                            CashRegister.Printer.EndSlip(null);
                            break;
                        }
                        break;

                    case "86":    //Last document info
                        CheckCashier();
                        bool zReport = false;
                        switch (orderLine.Substring(11, 3))
                        {
                        case "SBB":        //stands for "Son Belge Bilgisi"
                            zReport = false;
                            break;

                        case "SZB":        //stands for "Son Z Bilgisi"
                            zReport = true;
                            break;
                        }

                        PrintedDocumentInfo lastDocInfo = CashRegister.Printer.GetLastDocumentInfo(zReport);
                        SendMessage(String.Format("{0:D4}{1:D4}{2:D4}{3:D2}{4:yyyyMMddHHmm}",
                                                  lastDocInfo.ZNo,
                                                  lastDocInfo.EjNo,
                                                  lastDocInfo.DocId,
                                                  (int)lastDocInfo.Type,
                                                  lastDocInfo.DocDateTime));
                        break;

                    case "99":
                        try
                        {
                            listener.Stop();
                            threadClient.Abort();
                        }catch {}
                        retVal = TCPMsgType.TCP_MSG_EXIT;
                        break;

                    default: break;
                    }

                    buff[0] = ACK;
                    client.Send(buff);
                }
                catch (Exception ex)
                {
                    string errMsg = new Error(ex).Message;
                    if (ex is PosException && ((PosException)ex).ErrorCode > 0)
                    {
                        errMsg = String.Format("{0:D4}", ((PosException)ex).ErrorCode);
                    }
                    if (doc != null)
                    {
                        try
                        {
                            CashRegister.Printer.Void();
                            Data.Connector.Instance().OnDocumentVoided(doc, 1);
                        }
                        catch
                        {
                        }
                    }


                    String data = String.Format("{0}{1}{2:D" + DIGITS_OF_LEN + "}{3}{4}",
                                                (char)NAK,
                                                (char)STX,
                                                errMsg.Length + 1,
                                                errMsg,
                                                (char)ETX);

                    buff = System.Text.Encoding.UTF8.GetBytes(data);
                    client.Send(buff);
                    break;
                }
            }

            if (buff[0] == ACK)
            {
                buff[0] = EOT;
                client.Send(buff);
            }

            return(retVal);
        }
Esempio n. 3
0
 abstract public void Show(VoidItem vi);
Esempio n. 4
0
        private void ProcessOrder(String orderLines, Socket client)
        {
            String [] lines = orderLines.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            byte []   buff  = new byte[1];

            SalesDocument doc = null;

            foreach (String orderLine in lines)
            {
                try
                {
                    switch (orderLine.Substring(8, 2))
                    {
                    case "01":    //FIS
                    case "02":    //FAT,IAD,IRS
                        switch (orderLine.Substring(11, 3))
                        {
                        case PosMessage.HR_CODE_INVOICE:
                            doc = new Invoice(); break;

                        case PosMessage.HR_CODE_RETURN:
                            doc = new ReturnDocument(); break;

                        case PosMessage.HR_CODE_WAYBILL:
                            doc = new Waybill(); break;

                        default:
                            doc = new Receipt();
                            break;
                        }
                        CashRegister.Printer.AdjustPrinter(doc);
                        CashRegister.Printer.PrintHeader(doc);
                        //doc.SalesPerson = cr.DataConnector.FindCashierById(line.Substring(23, 4));
                        break;

                    case "03":    //TAR
                        //1,00011,03,TAR,25/03/2009  ,00:21:26
                        break;

                    case "04":    //SAT
                    case "05":
                        FiscalItem si = new SalesItem();

                        String  pluNo     = orderLine.Substring(21, 6);
                        decimal amount    = Convert.ToDecimal(orderLine.Substring(30, 10)) / 100m;
                        decimal quantity  = Convert.ToDecimal(orderLine.Substring(15, 6)) / 1000m;
                        int     depID     = Convert.ToInt32(orderLine.Substring(28, 2));
                        String  name      = orderLine.Substring(41, 20).Trim().ToUpper();
                        decimal unitPrice = Math.Round(amount / quantity, 2);

                        IProduct prd = Data.Connector.Instance().CreateProduct(name + "DYNAMIC", Department.Departments[depID], unitPrice);

                        si.Quantity    = quantity;
                        si.Product     = prd;
                        si.TotalAmount = amount;
                        si.UnitPrice   = unitPrice;

                        if (orderLine.Contains("SAT"))
                        {
                            CashRegister.Printer.Print(si);
                            doc.Items.Add(si);
                        }
                        else
                        {
                            VoidItem vi = new VoidItem(si);
                            CashRegister.Printer.Print(vi);
                            doc.Items.Add(vi);
                        }
                        break;

                    case "06":    //IND,ART
                        AdjustmentType adjustmentType = AdjustmentType.Discount;
                        int            percentage     = 0;
                        bool           isPercent      = Parser.TryInt(orderLine.Substring(25, 2), out percentage);

                        IAdjustable target = doc.Items[doc.Items.Count - 1];
                        if (orderLine.Substring(15, 3) == "TOP")
                        {
                            target = doc;
                            CashRegister.Printer.PrintSubTotal(doc, true);
                        }

                        if (isPercent)
                        {
                            adjustmentType = AdjustmentType.PercentDiscount;
                        }

                        if (orderLine.Substring(11, 3) == "ART")
                        {
                            adjustmentType = isPercent ? AdjustmentType.PercentFee : AdjustmentType.Fee;
                        }

                        decimal adjAmount = Convert.ToDecimal(orderLine.Substring(30, 10)) / 100m;
                        if (isPercent)
                        {
                            adjAmount = (decimal)percentage;
                        }
                        Adjustment adj = new Adjustment(target, adjustmentType, adjAmount);
                        CashRegister.Printer.Print(adj);

                        target.Adjust(adj);
                        break;

                    case "08":
                        doc.TotalAmount = Convert.ToDecimal(orderLine.Substring(30, 10)) / 100m;
                        //DocumentStatus status = (DocumentStatus)Convert.ToInt16(orderLine.Substring(26, 1));
                        CashRegister.Printer.PrintTotals(doc, true);
                        break;

                    case "09":
                        decimal payment = Convert.ToDecimal(orderLine.Substring(30, 10)) / 100m;
                        switch (orderLine.Substring(11, 3))
                        {
                        case "NAK":
                            CashRegister.Printer.Pay(payment);
                            doc.Payments.Add(new CashPaymentInfo(payment));
                            break;

                        case "CEK":
                            String refNum = orderLine.Substring(15, 12).Trim();
                            CashRegister.Printer.Pay(payment, refNum);
                            doc.Payments.Add(new CheckPaymentInfo(payment, refNum));
                            break;

                        case "DVZ":
                            String  exCode = orderLine.Substring(15, 1);
                            decimal exAmnt = Convert.ToDecimal(orderLine.Substring(18, 9)) / 100m;
                            String  exName = orderLine.Substring(41, 20);
                            int     currId = int.Parse(orderLine.Substring(62, 2));

                            ICurrency currency = Data.Connector.Instance().GetCurrencies()[currId];

                            CashRegister.Printer.Pay(payment, currency);
                            doc.Payments.Add(new CurrencyPaymentInfo(currency, payment));
                            break;
                        }
                        break;

                    case "10":
                        decimal crdPayment   = Convert.ToDecimal(orderLine.Substring(30, 10)) / 100m;
                        int     installments = int.Parse(orderLine.Substring(15, 2));
                        int     crdId        = int.Parse(orderLine.Substring(25, 2));
                        String  crdName      = orderLine.Substring(41, 20);

                        ICredit crd = Data.Connector.Instance().GetCredits()[crdId];

                        CashRegister.Printer.Pay(crdPayment, crd, installments);
                        doc.Payments.Add(new CreditPaymentInfo(crd, crdPayment));
                        break;

                    case "11":
                        CashRegister.Printer.PrintFooter(doc, true);
                        Data.Connector.Instance().OnDocumentClosed(doc);
                        break;

                    case "22":
                        String remark = orderLine.Substring(15, orderLine.Length - 15).TrimEnd();
                        CashRegister.Printer.PrintRemark(remark);
                        break;

                    default: break;
                    }

                    buff[0] = ACK;
                    client.Send(buff);
                }
                catch
                {
                    if (doc != null)
                    {
                        CashRegister.Printer.Void();
                        Data.Connector.Instance().OnDocumentVoided(doc, (int)doc.Status);
                    }
                    buff[0] = NAK;
                    client.Send(buff);
                    break;
                }
            }

            if (buff[0] == ACK)
            {
                buff[0] = EOT;
                client.Send(buff);
            }
            orderState = OrderState.IDLE;
        }