コード例 #1
0
ファイル: SalesOrder.cs プロジェクト: anthonied/LiquidPastel
        private void ProcessReturnItems(PsqlConnection oPasConn, PsqlConnection oSolConn, ref SalesLine slActive)
        {
            bool bInvoice = false;
            bool bUpdated = false;
            string sLine = "";
            string sSql = "";
            string[] aSalesRemoveLines = new string[0];
            int iIndex = 0;

            if (slActive.chkReturn.Checked)
            {
                //Validations
                if (Convert.ToDecimal(slActive.txtMultiplier.Text) > slActive.dMaxMultiplierValue)//check if multiplier returned is more than the original multiplier
                {
                    MessageBox.Show("You can't return more items than originally orderder. \r\n  The original order amout was: " + slActive.dMaxMultiplierValue.ToString(), "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    slActive.txtMultiplier.Parent.BackColor = Color.Red;
                    return;
                }
                else if (slActive.txtMultiplier.Text != "" && Convert.ToDecimal(slActive.txtMultiplier.Text) <= 0)
                {
                    MessageBox.Show("You can't return zero or negative item.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    slActive.txtMultiplier.Parent.BackColor = Color.Red;
                    return;
                }
                else
                {//process returned item

                    for (int iLines = 0; iLines < aSaleslines.Length; iLines++)   //find sales line that is linked to this entry
                    {

                        DateTime dtDeliveryDate = DateTime.Now, dtReturnDate = DateTime.Now;
                        int iRet = 0;
                        bInvoice = true;
                        bUpdated = true;
                        if (slActive.sLineType == "1")//lease item
                        {
                            dtDeliveryDate = slActive.dtDelivery.Value;
                            dtReturnDate = slActive.dtReturnDate.Value;

                            sSql = "UPDATE Inventory SET "; //book this item back for rental
                            sSql += " UserDefText01 = '' ";
                            sSql += ", UserDefText02 = '' ";
                            sSql += ", UserDefText03 = '' ";
                            sSql += " WHERE ItemCode = '" + slActive.txtCode.Text + "' ";
                            iRet = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oPasConn).ExecuteNonQuery();
                        }
                        if (slActive.sLineType != "Comment")
                        {
                            string sTotalQty = (Convert.ToDecimal(slActive.txtMultiplier.Text) * Convert.ToDecimal(slActive.txtQuantity.Text)).ToString("N2"); //Update Pastel directly because if updated through the SDK it gets a new linkedNum
                            sSql = "UPDATE HistoryLines SET ";
                            sSql += " Qty = " + sTotalQty.Replace(",", "");
                            sSql += ", UnitPrice = " + slActive.txtExcPrice.Text.Replace(",", "");
                            sSql += ", DiscountPercentage = " + (Convert.ToDecimal(slActive.txtDiscount.Text.Replace(",", "")) * 100).ToString();
                            sSql += " where DocumentNumber = '" + txtNumber.Text.Trim() + "' and ItemCode = '" + slActive.txtCode.Text.Trim() + "' and LinkNum = " + slActive.sPastelLineLink;
                            iRet = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oPasConn).ExecuteNonQuery();

                            sSql = "UPDATE SOLHL SET "; //mark salesline as returned
                            sSql += " Status = 1";
                            sSql += ", Multiplier = " + slActive.txtMultiplier.Text.Replace(",", "");
                            sSql += ", Qty = " + slActive.txtQuantity.Text.Replace(",", "");
                            if (slActive.sLineType == "1")//lease item
                            {
                                sSql += ", DeliveryDate = '" + dtDeliveryDate.ToString("dd-MM-yyyy") + "'";
                                sSql += ", ReturnDate = '" + dtReturnDate.ToString("dd-MM-yyyy") + "'";
                            }
                            sSql += " WHERE Header = '" + txtNumber.Text + "' and ItemCode = '" + slActive.txtCode.Text.Trim() + "' and LinkNum = " + slActive.sPastelLineLink;
                            iRet = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oSolConn).ExecuteNonQuery();
                            slActive.txtStatus.Text = "1";
                            addTotals();
                        }
                        #region Multiplier Test And Setup
                        //Multiplier returns
                        //If less items is returned in the multiplier field Add the remaining qty
                        bool bMultiplierPartialReturn = false;
                        string sNewLinkNum = "";
                        string[] aMultiplierDownTime = new string[0];

                        if (Convert.ToDecimal(slActive.txtMultiplier.Text) < slActive.dMaxMultiplierValue && slActive.sLineType == "1")//Less items returned and must be lease item
                        {
                            bMultiplierPartialReturn = true;
                        }

                        #endregion

                        #region Multiplier Function
                        //Multiplier returns
                        //If less items is returned in the multiplier field Add the remaining qty
                        if (bMultiplierPartialReturn)
                        {
                            string sNewQty = (slActive.dMaxMultiplierValue - Convert.ToDecimal(slActive.txtMultiplier.Text) * Convert.ToDecimal(slActive.txtQuantity.Text)).ToString();
                            //Book linked item back via invoice
                            sLine = "|"; //Cost Price
                            sLine += sNewQty + "|"; //Line Quantity
                            sLine += slActive.txtExcPrice.Text + "|"; //Exclusive Price Per Unit
                            if (slActive.txtTaxType.Text == "0")
                                sLine += (Convert.ToDecimal(slActive.txtExcPrice.Text)).ToString().Replace(",", "") + "|"; //Inclusive Price Per Unit
                            else if (slActive.txtTaxType.Text == "1")
                                sLine += (Convert.ToDecimal(slActive.txtExcPrice.Text) * 1.14m).ToString().Replace(",", "") + "|"; //Inclusive Price Per Unit
                            sLine += slActive.txtUnit.Text + "|"; //Unit
                            sLine += slActive.txtTaxType.Text + "|"; //Tax Type
                            sLine += slActive.txtDiscountType.Text + "|"; //Discount Type
                            sLine += slActive.txtDiscount.Text + "|"; //Discount %
                            sLine += slActive.txtCode.Text + "|"; //Code
                            sLine += slActive.txtDescription.Text + "|"; //Description
                            sLine += "4|"; //Line Type
                            sLine += slActive.txtStore.Text + "|"; //MultiStore
                            sLine += "|"; //CostCode
                            string[] aMultiplierReturn = clsSDK.EditPastelDocument(sLine, 102, txtNumber.Text, "I", Global.sDataPath).Split("|".ToCharArray());
                            if (aMultiplierReturn[0] != "0")
                            {
                                MessageBox.Show("Error 100.  Failed to create new multiplier row. \r\n\r\n Error " + aMultiplierReturn[1], "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                            else //Pastel row added
                            {
                                //Get new Linknum for line - must get from Pastel because downtime lines and notes is not stored in SolHL
                                sSql = "Select max(LinkNum) from HistoryLines where DocumentNumber = '" + txtNumber.Text + "'";
                                sNewLinkNum = Connect.getDataCommand(sSql, oPasConn).ExecuteScalar().ToString();
                                sSql = "INSERT into SOLHL";
                                sSql += " (Header, ItemCode, DeliveryDate, ReturnDate, Status,LinkNum, Multiplier, Qty) ";
                                sSql += " VALUES ";
                                sSql += "(";
                                sSql += "'" + txtNumber.Text.Trim() + "'";
                                sSql += ",'" + slActive.txtCode.Text + "'";
                                sSql += ", '" + slActive.dtDelivery.Value.ToString("dd-MM-yyyy") + "'";
                                sSql += ",'" + slActive.dtReturnDate.Value.ToString("dd-MM-yyyy") + "'";
                                sSql += ",0"; //status0 not returned
                                sSql += "," + sNewLinkNum;
                                sSql += "," + (slActive.dMaxMultiplierValue - Convert.ToDecimal(slActive.txtMultiplier.Text)).ToString();
                                sSql += "," + slActive.txtQuantity.Text;
                                sSql += ")";
                                int Ret = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oSolConn).ExecuteNonQuery();
                                //Add Downtime to new row
                                for (int i = 0; i < aMultiplierDownTime.Length; i++)
                                {
                                    if (aMultiplierDownTime[i].Trim() != "")
                                    {
                                        string[] aEditDownReturn = clsSDK.EditPastelDocument(aMultiplierDownTime[i].Replace("~~~", sNewLinkNum.PadLeft(3, "0".ToCharArray()[0])), 102, txtNumber.Text, "I", Global.sDataPath).Split("|".ToCharArray());
                                    }
                                }
                            }
                        }
                        #endregion

                        iIndex++;
                        break;
                    }
                }
            }
            if (!bUpdated)
            {
              //  MessageBox.Show("No items were selected for return.", "No Returned Items", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (Global.bAutoInvoiceOnReturn && bInvoice)//Create Invoice for Returned items
                {
                    Cursor = System.Windows.Forms.Cursors.WaitCursor;
                    using (Solsage_Process_Management_System.Forms.InvoiceOrder frmInvoice = new Solsage_Process_Management_System.Forms.InvoiceOrder())
                    {
                        frmInvoice.frmSalesOrder = this;
                        frmInvoice.sDocNumber = txtNumber.Text;
                        frmInvoice.sCustomerCode = txtCustomerCode.Text;
                        frmInvoice.sCustomerDescription = txtCustomerDescription.Text;
                        frmInvoice.sDiscountPerc = txtDiscount.Text;
                        frmInvoice.sDeliveryDate = dtDeliveryDate.Value.ToString("dd/MM/yyyy");
                        frmInvoice.sOrderNumber = txtOrderNumber.Text;
                        frmInvoice.sSiteName = selAddresses.Text.Trim();
                        frmInvoice.frmMain = ((Main)this.MdiParent);
                        //autoinvoice
                        if (chkChangeInvoice.Checked)
                        {
                            chkChangeInvoice.Checked = false;
                            if (frmInvoice.ShowDialog() == DialogResult.OK)
                            {
                                loadSalesOrder(txtNumber.Text);
                            }
                            else//Closed application
                            {
                                frmInvoice.InvoiceOrder_Load(null, null);
                                frmInvoice.cmdCreateInvoice_Click(null, null);
                                loadSalesOrder(txtNumber.Text);
                            }
                        }
                        else
                        {
                            frmInvoice.InvoiceOrder_Load(null, null);
                            frmInvoice.cmdCreateInvoice_Click(null, null);
                            loadSalesOrder(txtNumber.Text);
                        }
                    }
                    Cursor = System.Windows.Forms.Cursors.Default;
                }
            }
        }