Exemple #1
0
        public JsonResult PostAddContractAmountLast(Contract item)
        {
            try
            {
                if (Session["iuser"] == null)
                {
                    throw new Exception(" Session หมดอายุ , กรุณาเข้าสู่ระบบใหม่อีกครั้ง !! ");
                }

                item.ContractInsertBy = (Int32)Session["iuser"];

                IList <Contract> listContract = new List <Contract>();
                ContractData     cd           = new ContractData();
                listContract = cd.GetContract(item.ContractCustomerID, item.ContractID);
                item         = new Contract();
                item         = listContract[0];
                ProductData pd = new ProductData();
                pd.AddDailyReceipt(item.ContractPayment, item.ContractAmountLast, item.ContractCustomerID, item.ContractID);


                return(Json(new
                {
                    data = "บันทึกค่างวดแรกสำเร็จ",
                    success = true
                }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                return(Json(new
                {
                    data = ex.Message,
                    success = false
                }, JsonRequestBehavior.AllowGet));
            }
        }
Exemple #2
0
        // GET: /Customers/GetContract?CustomerID=id&ContractID=id
        public JsonResult GetContract(int CustomerID, int ContractID)
        {
            try
            {
                IList <Contract> listContract = new List <Contract>();
                ContractData     cd           = new ContractData();
                listContract = cd.GetContract(CustomerID, ContractID);

                IList <Customers> listData = new List <Customers>();
                CustomersData     dataCus  = new CustomersData();
                listData = dataCus.Get(CustomerID);

                ProductData           dataPro            = new ProductData();
                IList <ProductSelect> listProductsSelect = new List <ProductSelect>();

                IList <Products> listProduct = new List <Products>();
                listProduct = dataPro.GetListProduct();


                List <ProductSelect> listProductsSelect1 = new List <ProductSelect>();

                if (ContractID > 0)
                {
                    listProductsSelect = dataPro.GetProductCustomer(CustomerID, ContractID);
                }
                double ContractPayment = 0;
                listProductsSelect1 = dataPro.ProductContractSummary(ref ContractPayment, listProductsSelect);



                return(Json(new
                {
                    data = listContract,
                    dataCustomers = listData,
                    dataProduct = listProduct,
                    dataProductSelect = listProductsSelect1,
                    success = true
                }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                return(Json(new
                {
                    data = ex.Message,
                    success = false
                }, JsonRequestBehavior.AllowGet));
            }
        }
        public void PaymentDailyReceipts2(DailyReceiptsReport item)
        {
            if (item.PriceReceipts <= 0)
            {
                throw new Exception("จำนวนเงินต้องมากว่า 0 บาท");
            }

            string           StrSql = string.Empty;
            ContractData     ct     = new ContractData();
            IList <Contract> list   = new List <Contract>();

            list = ct.GetContract(item.CustomerID, item.ContractID);
            Contract contract = new Contract();

            contract = list.FirstOrDefault();


            //get วันที่จ่ายล่าสุด
            ReportData data = new ReportData();
            IList <LastTransaction> listData1 = new List <LastTransaction>();

            listData1 = data.GetTransaction(item.StaffID.ToString(), item.CustomerID.ToString(), item.ContractID.ToString());
            listData1 = listData1.OrderByDescending(c => c.DateAsOf).ToArray();

            MySqlConnection ObjConn = DBHelper.ConnectDb(ref errMsg);

            try
            {
                DateTime LastDate = listData1.FirstOrDefault().DateAsOf; //วันที่จ่านล่าสุด
                decimal  LastPay  = listData1.FirstOrDefault().Amount;   //วันที่จ่านล่าสุด

                decimal totalsales = contract.ContractPayment;           // ยอดเงินทั้งหมด
                decimal rate       = contract.ContractInterest;          //ดอกเบี้ย
                decimal amount     = contract.ContractAmount;            //จำนวนที่จ่ายรายวัน
                decimal interest   = item.PriceReceipts * (rate / 100);  //ดอกเบี้ย
                decimal Priciple   = item.PriceReceipts - interest;      // เงินต้น


                if (item.PriceReceipts <= amount)   // จ่ายพอดี
                {
                    item.ID = Utility.GetMaxID("daily_receipts", "ID");
                    StrSql  = @" INSERT INTO daily_receipts(ID,CustomerID,ContractID,DateAsOf,
            TotalSales,PriceReceipts,Principle,Interest,StaffID,Latitude,Longitude,Activated,Deleted)
            VALUES("

                              + item.ID + ","
                              + item.CustomerID + ","
                              + item.ContractID + ","
                              + Utility.FormateDateTime(DateTime.Now) + ","
                              + totalsales + ","
                              + item.PriceReceipts + ","
                              + Priciple + ","
                              + interest + ","
                              + item.StaffID + ","
                              + item.Latitude + ","
                              + item.Longitude + ","
                              + 0 + ","
                              + 0 + ");";

                    DBHelper.Execute(StrSql, ObjConn);
                }
                else if (item.PriceReceipts > amount)   // จ่ายเกิน
                {
                    decimal price = item.PriceReceipts;
                    int     k     = 0;
                    while (price >= amount)
                    {
                        item.ID = Utility.GetMaxID("daily_receipts", "ID");
                        StrSql  = @" INSERT INTO daily_receipts(ID,CustomerID,ContractID,DateAsOf,
            TotalSales,PriceReceipts,Principle,Interest,StaffID,Latitude,Longitude,Activated,Deleted)
            VALUES("

                                  + item.ID + ","
                                  + item.CustomerID + ","
                                  + item.ContractID + ","
                                  + Utility.FormateDateTime(DateTime.Now.AddDays(k)) + ","
                                  + totalsales + ","
                                  + amount + ","
                                  + (amount - (amount * (rate / 100))) + ","
                                  + (amount * (rate / 100)) + ","
                                  + item.StaffID + ","
                                  + item.Latitude + ","
                                  + item.Longitude + ","
                                  + 0 + ","
                                  + 0 + ");";

                        DBHelper.Execute(StrSql, ObjConn);

                        price = price - amount;
                        k     = k + 1;


                        //เหลือเศษ
                        if (price < amount)
                        {
                            item.ID = Utility.GetMaxID("daily_receipts", "ID");
                            StrSql  = @" INSERT INTO daily_receipts(ID,CustomerID,ContractID,DateAsOf,
            TotalSales,PriceReceipts,Principle,Interest,StaffID,Latitude,Longitude,Activated,Deleted)
            VALUES("

                                      + item.ID + ","
                                      + item.CustomerID + ","
                                      + item.ContractID + ","
                                      + Utility.FormateDateTime(DateTime.Now.AddDays(k)) + "," // Get วัน อนาคต
                                      + totalsales + ","
                                      + price + ","
                                      + (price - (price * (rate / 100))) + ","
                                      + (price * (rate / 100)) + ","
                                      + item.StaffID + ","
                                      + item.Latitude + ","
                                      + item.Longitude + ","
                                      + 0 + ","
                                      + 0 + ");";
                            DBHelper.Execute(StrSql, ObjConn);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally {
                ObjConn.Close();
            }
        }
        public void PaymentDailyReceipts(DailyReceiptsReport item)
        {
            if (item.PriceReceipts <= 0)
            {
                throw new Exception("จำนวนเงินต้องมากว่า 0 บาท");
            }


            string           StrSql = string.Empty;
            ContractData     ct     = new ContractData();
            IList <Contract> list   = new List <Contract>();

            list = ct.GetContract(item.CustomerID, item.ContractID);
            Contract contract = new Contract();

            contract = list.FirstOrDefault();

            decimal totalsales   = contract.ContractPayment;  // ยอดเงินทั้งหมด
            decimal rate         = contract.ContractInterest; //ดอกเบี้ย
            decimal amount       = contract.ContractAmount;   //จำนวนที่จ่ายรายวัน
            decimal priceReciept = item.PriceReceipts;

            decimal interest = item.PriceReceipts * (rate / 100); //ดอกเบี้ย
            decimal Priciple = item.PriceReceipts - interest;     // เงินต้น


            DataTable dt = new DataTable();

            MySqlConnection ObjConn = DBHelper.ConnectDb(ref errMsg);

            dt = DBHelper.List("select * FROM daily_receipts WHERE ContractID=" + item.ContractID +
                               "  and PriceReceipts!=Principle  order by PriceReceipts desc",
                               ObjConn);

            if (dt.Rows.Count > 0)
            {
                // ข้อมูลการจ่ายทั้งหมด
                int TotalReceiptID = Utility.GetMaxID("daily_totalreceipts", "TotalReceiptID");
                StrSql = @" INSERT INTO daily_totalreceipts(TotalReceiptID,CustomerID,ContractID,DateAsOf,
                TotalSales,PriceReceipts,Principle,Interest,StaffID,Activated,Deleted)
                VALUES("
                         + TotalReceiptID + ","
                         + item.CustomerID + ","
                         + item.ContractID + ","
                         + Utility.FormateDateTime(DateTime.Now) + ","
                         + totalsales + ","
                         + item.PriceReceipts + ","
                         + (item.PriceReceipts - (item.PriceReceipts * (rate / 100))) + ","
                         + (item.PriceReceipts * (rate / 100)) + ","
                         + item.StaffID + ","
                         + 1 + ","
                         + 0 +
                         ");";

                DBHelper.Execute(StrSql, ObjConn);

                decimal price = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (priceReciept > 0)
                    {
                        //ครั้งก่อนยังจ่ายไม่เต็ม
                        if (Convert.ToDecimal(dt.Rows[i]["PriceReceipts"]) < amount)
                        {
                            //จำนวนเงินที่ขาด
                            price = (amount - Convert.ToDecimal(dt.Rows[i]["PriceReceipts"]));
                            if (priceReciept < price)
                            {
                                DBHelper.List("UPDATE daily_receipts SET " +
                                              "  PriceReceipts = PriceReceipts+" + priceReciept +
                                              "  ,StaffID =" + item.StaffID +
                                              "  ,Interest =" + interest +
                                              "   WHERE ContractID =" + item.ContractID + " and  DateAsOf='" + Convert.ToDateTime(dt.Rows[i]["DateAsOf"]).ToString(@"yyyy/MM/dd") + "'", ObjConn);
                                priceReciept = priceReciept - price;
                            }
                            else if (priceReciept >= price)
                            {
                                DBHelper.List("UPDATE daily_receipts SET " +
                                              "  PriceReceipts = PriceReceipts+" + price +
                                              "  ,StaffID =" + item.StaffID +
                                              "  ,Interest =" + interest +
                                              "   WHERE ContractID =" + item.ContractID + " and  DateAsOf='" + Convert.ToDateTime(dt.Rows[i]["DateAsOf"]).ToString(@"yyyy/MM/dd") + "'", ObjConn);
                                priceReciept = priceReciept - price;
                            }

                            //if (priceReciept >= amount)
                            //{
                            //    price = (amount - Convert.ToDecimal(dt.Rows[i]["PriceReceipts"]));
                            //    priceReciept = priceReciept - price;
                            //    DBHelper.List("UPDATE daily_receipts SET " +
                            //     "  PriceReceipts = PriceReceipts+" + price +
                            //     "  ,StaffID =" + item.StaffID +
                            //     "  ,Interest =" + interest +
                            //     "   WHERE ContractID =" + item.ContractID + " and  DateAsOf='" + Convert.ToDateTime(dt.Rows[i]["DateAsOf"]).ToString(@"yyyy/MM/dd") + "'", ObjConn);

                            //}
                            //else if (priceReciept< amount)   //เงินไม่พอ
                            //{
                            //    // เช็ค ตัวที่ยังไม่ครบ
                            //    if (Convert.ToDecimal(dt.Rows[i]["PriceReceipts"]) < amount){
                            //        if (priceReciept < amount) {
                            //            price = (amount - Convert.ToDecimal(dt.Rows[i]["PriceReceipts"]);

                            //        }
                            //        priceReciept = priceReciept - Convert.ToDecimal(dt.Rows[i]["PriceReceipts"]);

                            //        DBHelper.List("UPDATE daily_receipts SET " +
                            //         "  PriceReceipts = PriceReceipts + " + priceReciept +
                            //         "  ,StaffID =" + item.StaffID +
                            //         "  ,Interest =" + interest +
                            //         "   WHERE ContractID =" + item.ContractID + " and  DateAsOf='" + Convert.ToDateTime(dt.Rows[i]["DateAsOf"]).ToString(@"yyyy/MM/dd") + "'", ObjConn);

                            //    }

                            //    DBHelper.List("UPDATE daily_receipts SET " +
                            //     "  PriceReceipts = PriceReceipts + " + priceReciept +
                            //     "  ,StaffID =" + item.StaffID +
                            //     "  ,Interest =" + interest +
                            //     "   WHERE ContractID =" + item.ContractID + " and  DateAsOf='" + Convert.ToDateTime(dt.Rows[i]["DateAsOf"]).ToString(@"yyyy/MM/dd") + "'", ObjConn);
                            //    priceReciept = priceReciept - priceReciept;
                            //}
                        }//ปัจจุบัน
                    }
                    else
                    {
                        i = dt.Rows.Count;
                    }
                } // loop for
            }
        }