示例#1
0
        public async Task <IHttpActionResult> Create(invoiceDetailListDTO newDTO)
        {
            sapi.db db = new sapi.db();
            try
            {
                db.connect();
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                string token = "";
                System.Net.Http.Headers.HttpRequestHeaders headers = this.Request.Headers;
                if (headers.Contains("token"))
                {
                    foreach (var s in headers.GetValues("token"))
                    {
                        token = s;
                    }
                }
                return(Ok(await xcrm.UploadInvoice(db, newDTO, token)));
                //return Ok(await repository.Create(saleOrderItem, token));
            }
            catch (HttpException ex)
            {
                return(BadRequest(ex.Message));
            }
            finally
            {
                db.close();
            }
        }
示例#2
0
        public static async Task <string> UploadInvoice(sapi.db db, invoiceDetailListDTO newDTOs, string token)
        {
            Dictionary <string, string> vals = new Dictionary <string, string>();
            var users = Helper.GetUserProfile(token);

            if (users == null)
            {
                throw new HttpException((int)HttpStatusCode.Unauthorized, "Invalid Token !");
            }

            if (newDTOs.results.Count <= 0)
            {
                throw new HttpException((int)HttpStatusCode.NotFound, "No Item(s) !");
            }

            DataTable tblSetting = db.readData("Select * from sys_Setting");

            DataTable tblSalesman = db.readData("Select * from tblSalesman Where salm_SalesmanID = " + db.cNum(users.user_SalesmanID.ToString()));

            if (tblSalesman.Rows.Count <= 0)
            {
                if (tblSetting.Rows[0]["sett_useSalesman"].ToString().ToLower() == "y")
                {
                    throw new HttpException((int)HttpStatusCode.NotFound, "Salesman Not Found !");
                }
            }
            else
            {
                var ttt = tblSetting.Rows[0]["sett_useProvince"].ToString().ToLower();
                if (tblSetting.Rows[0]["sett_useProvince"].ToString().ToLower() == "y")
                {
                    if (String.IsNullOrEmpty(tblSalesman.Rows[0]["salm_Province"].ToString()))
                    {
                        throw new HttpException((int)HttpStatusCode.NotFound, "Salesman's province Not Found !");
                    }
                }
            }

            DataTable tblSett = db.readData("select * from sys_setting");

            if (tblSett.Rows.Count <= 0)
            {
                throw new HttpException((int)HttpStatusCode.NotFound, "Warehouse not Found !");
            }
            string invo_WarehouseID = "";

            foreach (DataRow row in tblSett.Rows)
            {
                invo_WarehouseID = row["sett_WarehouseID"].ToString();
                if (string.IsNullOrEmpty(invo_WarehouseID))
                {
                    throw new HttpException((int)HttpStatusCode.NotFound, "Warehouse not Found !");
                }
            }

            HttpContext.Current.Session["userid"] = users.id;
            HttpContext.Current.Session["user"]   = users.userName;
            string re     = "";
            string re2    = "";
            string hid    = "";
            string errStr = "";

            sapi.sapi cls                    = new sapi.sapi();
            string    screenItem             = "tblInvoiceItemNew";
            string    screen                 = "tblInvoiceNew";
            Dictionary <string, string> aVal = new Dictionary <string, string>();
            DataTable tblResult              = new DataTable();

            tblResult.Rows.Add();
            tblResult.Columns.Add("status");
            tblResult.Columns.Add("msg");

            if (db.connect())
            {
                string salm_SalesmanID = "";
                string salm_Province   = "";
                if (tblSalesman.Rows.Count > 0)
                {
                    salm_SalesmanID = tblSalesman.Rows[0]["salm_SalesmanID"].ToString();
                    salm_Province   = tblSalesman.Rows[0]["salm_Province"].ToString();
                }
                string    invo_PriceListID = newDTOs.invo_PriceListID.ToString();
                DataTable tblCust          = db.readData("Select * from tblCustomer Where cust_CustomerID = " + db.cNum(newDTOs.invo_CustomerID.ToString()));
                foreach (DataRow row in tblCust.Rows)
                {
                    invo_PriceListID = row["cust_PriceListID"].ToString();
                }


                vals.Add("invo_Date".ToLower(), newDTOs.invo_Date?.ToString("dd/MM/yyyy"));
                vals.Add("invo_Province".ToLower(), salm_Province);
                vals.Add("invo_Company".ToLower(), newDTOs.invo_Company);
                vals.Add("invo_CustomerID".ToLower(), newDTOs.invo_CustomerID.ToString());
                vals.Add("invo_PriceListID".ToLower(), invo_PriceListID);
                vals.Add("invo_WarehouseID".ToLower(), invo_WarehouseID);
                vals.Add("invo_SalesmanID".ToLower(), salm_SalesmanID);
                vals.Add("invo_Status".ToLower(), "New");

                string    wh    = "";
                string    mbid  = "";
                DataTable tblMB = db.readData("select * from tblMobileSale " +
                                              " inner join tblMobileSaleItem on msit_MobileSaleID = mbsl_MobileSaleID and msit_Deleted is null " +
                                              " where mbsl_CheckIn is null and msit_UserID = " + users.id);
                foreach (DataRow rowMB in tblMB.Rows)
                {
                    wh   = rowMB["mbsl_warehouseID"].ToString();
                    mbid = rowMB["mbsl_MobileSaleID"].ToString();
                    vals["invo_WarehouseID".ToLower()] = wh;
                    aVal.Add("invo_MobileSaleID", mbid);
                }



                foreach (var item in newDTOs.results)
                {
                    decimal?qty = 0;
                    foreach (var item1 in newDTOs.results)
                    {
                        if (item.init_ItemID == item1.init_ItemID)
                        {
                            qty = qty + item1.init_Qty;
                        }
                    }
                    errStr += stockVerification(db, cls, item.init_ItemID.ToString(), (double)qty, newDTOs.invo_WarehouseID.ToString());
                }
                if (errStr.Length > 0)
                {
                    db.close();
                    tblResult.Rows[0]["status"] = "error";
                    tblResult.Rows[0]["msg"]    = errStr;
                    return(db.tblToJson(tblResult));
                }

                bool isCredit = false;
                if (vals.ContainsKey("isCredit".ToLower()))
                {
                    isCredit = true;
                }
                db.beginTran();
                if (!vals.ContainsKey("invo_invoiceid"))
                {
                    aVal.Add("invo_WorkflowID", "6");
                    aVal.Add("invo_WorkflowItemID", "12");
                }

                if (vals.ContainsKey("invo_exrate"))
                {
                    aVal.Add("invo_ExRate", vals["invo_exrate".ToLower()]);
                    vals.Remove("invo_exrate".ToLower());
                }
                if (vals.ContainsKey("invo_cashin"))
                {
                    aVal.Add("invo_CashIn", vals["invo_cashin".ToLower()]);
                    vals.Remove("invo_CashIn".ToLower());
                }
                if (vals.ContainsKey("invo_cashin2"))
                {
                    aVal.Add("invo_CashIn2", vals["invo_cashin2".ToLower()]);
                    vals.Remove("invo_cashin2".ToLower());
                }

                if (vals.ContainsKey("invo_status"))
                {
                    vals["invo_status"] = "completed";
                }

                if (vals.ContainsKey("invo_MobileSaleID".ToLower()))
                {
                    aVal.Add("invo_MobileSaleID", vals["invo_MobileSaleID".ToLower()]);
                    vals.Remove("invo_MobileSaleID".ToLower());
                }

                if (isCredit)
                {
                    aVal["invo_CashIn"]  = "0";
                    aVal["invo_CashIn2"] = "0";
                }
                if (!vals.ContainsKey("invo_Date".ToLower()))
                {
                    vals.Add("invo_Date".ToLower(), db.getDate(DateTime.UtcNow.AddHours(7).ToString("yyyy-MM-dd HH:mm"), 1));
                }
                else
                {
                    vals.Remove("invo_Date".ToLower());
                    vals.Add("invo_Date".ToLower(), db.getDate(DateTime.UtcNow.AddHours(7).ToString("yyyy-MM-dd HH:mm"), 1));
                }

                re = cls.saveRecord("tblInvoiceNew", vals, db, aVals: aVal, ignoreROF: true);

                var str = JsonConvert.DeserializeObject <dynamic>(re);
                if (str.tbl != null)
                {
                    if (str.tbl[0].status == "ok")
                    {
                        hid = (string)str.tbl[0].msg;
                        if (!vals.ContainsKey("invo_invoiceid"))
                        {
                            if (vals.ContainsKey("invo_customerid"))
                            {
                                if (!string.IsNullOrEmpty(vals["invo_customerid"]))
                                {
                                    var tmp = db.execData("Update tblCustomer Set cust_Type='Customer',cust_LastTransDate=GETDATE() Where /*cust_Type='Lead' and*/ cust_CustomerID=" +
                                                          vals["invo_customerid"].ToString());
                                    if (tmp != "ok")
                                    {
                                        db.rollback();
                                        throw new HttpException((int)HttpStatusCode.Unauthorized, tmp);
                                    }
                                }
                            }
                        }

                        foreach (var item in newDTOs.results)
                        {
                            Dictionary <string, string> iVals = new Dictionary <string, string>();
                            aVal.Clear();
                            aVal.Add("init_InvoiceID", hid);
                            iVals.Add("init_ItemID".ToLower(), item.init_ItemID.ToString());
                            iVals.Add("init_Description".ToLower(), item.init_Description);
                            iVals.Add("init_Qty".ToLower(), item.init_Qty.ToString());
                            iVals.Add("init_Price".ToLower(), item.init_Price.ToString());
                            iVals.Add("init_Total".ToLower(), (item.init_Price * item.init_Qty).ToString());
                            iVals.Add("init_WarehouseID".ToLower(), newDTOs.invo_WarehouseID.ToString());

                            aVal.Add("init_RPrice", item.init_Price.ToString());
                            aVal.Add("init_BQty", item.init_Price.ToString());

                            re = stockDeduction(db, item.init_ItemID.ToString(), newDTOs.invo_WarehouseID.ToString(), (double)item.init_Qty);
                            if (re == "")
                            {
                                re  = cls.saveRecord("tblInvoiceItemNew", iVals, db, aVal, ignoreROF: true);
                                str = JsonConvert.DeserializeObject <dynamic>(re);
                                if (str.tbl != null)
                                {
                                    if (str.tbl[0].status != "ok")
                                    {
                                        db.rollback();
                                        throw new HttpException((int)HttpStatusCode.Unauthorized, "Unable To Save Line Item !");
                                    }
                                }
                            }
                            else
                            {
                                db.rollback();
                                throw new HttpException((int)HttpStatusCode.Unauthorized, "Erro Validate Stock !");
                            }
                        }

                        invoiceTotal(hid, db);
                    }
                }
                db.commit();
                re = hid;
            }
            return(re);
        }