Ejemplo n.º 1
0
        public override Int64 Set(string parametersArr)
        {
            List <Foodics.NetSuite.Shared.Model.PaymentMethodEntity> lstitemsAll = new CustomDAO().SelectCustomerPayment(4);
            int Exe_length = 200;
            int lstend     = Exe_length;

            if (lstitemsAll.Count > 0)
            {
                for (int Index = 0; Index < lstitemsAll.Count; Index += Exe_length)
                {
                    if (Index + Exe_length >= lstitemsAll.Count)
                    {
                        lstend = lstitemsAll.Count - Index;
                    }
                    List <Foodics.NetSuite.Shared.Model.PaymentMethodEntity> invoiceMethodLst = lstitemsAll.GetRange(Index, lstend);
                    try
                    {
                        if (invoiceMethodLst.Count > 0)
                        {
                            List <Record> cps      = new List <Record>();
                            bool          is_valid = false;
                            try
                            {
                                for (int f = 0; f < invoiceMethodLst.Count; f++)
                                {
                                    PaymentMethodEntity payobj = invoiceMethodLst[f];
                                    Foodics.NetSuite.Shared.Model.Invoice invoiceobj = new GenericeDAO <Foodics.NetSuite.Shared.Model.Invoice>().GetWhere(" Foodics_Id = '" + payobj.Foodics_Id + "'").FirstOrDefault();
                                    Setting objSetting = new GenericeDAO <Setting>().GetWhere("Subsidiary_Netsuite_Id=" + invoiceobj.Subsidiary_Id).FirstOrDefault();
                                    #region Accept Payment
                                    CustomerPaymentApplyList    AplyList    = new CustomerPaymentApplyList();
                                    CustomerPaymentCreditList   CreditList  = new CustomerPaymentCreditList();
                                    CustomerPaymentDepositList  DepositList = new CustomerPaymentDepositList();
                                    List <CustomerPaymentApply> payApplylst = new List <CustomerPaymentApply>();
                                    CustomerPaymentApply        payApply;
                                    CustomerPayment             cp = new CustomerPayment();
                                    cp.autoApply = false;
                                    is_valid     = false;
                                    #region Payment Properties
                                    //customer
                                    RecordRef customer = new RecordRef();
                                    customer.internalId = invoiceobj.Customer_Netsuite_Id > 0 ? invoiceobj.Customer_Netsuite_Id.ToString() : objSetting.Customer_Netsuite_Id.ToString();
                                    customer.type       = RecordType.customer;
                                    cp.customer         = customer;

                                    //currency
                                    RecordRef currency = new RecordRef();
                                    currency.internalId = objSetting.Currency_Netsuite_Id.ToString();//payobj.Currency_Id.ToString();
                                    currency.type       = RecordType.currency;
                                    cp.currency         = currency;
                                    StringCustomFieldRef FoodicsRef, FoodicsNumb;

                                    // memo
                                    cp.memo = payobj.Notes;

                                    //tranDate
                                    cp.tranDate          = TimeZoneInfo.ConvertTimeToUtc(payobj.Business_Date, TimeZoneInfo.Local);
                                    cp.tranDateSpecified = true;

                                    //cp.subsidiary
                                    RecordRef subsidiary = new RecordRef();
                                    subsidiary.internalId = objSetting.Subsidiary_Netsuite_Id.ToString(); //payobj.Subsidiary_Id.ToString();
                                    subsidiary.type       = RecordType.subsidiary;
                                    cp.subsidiary         = subsidiary;

                                    //cp.location
                                    RecordRef location = new RecordRef();
                                    location.internalId = invoiceobj.Location_Id.ToString();//objSetting.Location_Netsuite_Id.ToString();//payobj.Location_Id.ToString();
                                    location.type       = RecordType.location;
                                    cp.location         = location;

                                    // payment amount
                                    cp.payment          = payobj.Amount;
                                    cp.paymentSpecified = true;



                                    #region Payment Custom Attributes

                                    DoubleCustomFieldRef payPercent = new DoubleCustomFieldRef();
                                    payPercent.scriptId = "custbody_da_payment_method_percentage";
                                    payPercent.value    = payobj.Payment_Method_Percentage;

                                    DoubleCustomFieldRef PayPercentAmount = new DoubleCustomFieldRef();
                                    PayPercentAmount.scriptId = "custbody_da_payment_method_amount";
                                    PayPercentAmount.value    = (payobj.Payment_Method_Percentage * payobj.Amount) / 100;

                                    LongCustomFieldRef trans_id = new LongCustomFieldRef();
                                    trans_id.scriptId = "custbody_da_transaction_id";
                                    trans_id.value    = invoiceobj.Netsuite_Id;

                                    FoodicsRef          = new StringCustomFieldRef();
                                    FoodicsRef.scriptId = "custbody_da_foodics_reference";
                                    FoodicsRef.value    = invoiceobj.BarCode.ToString();

                                    FoodicsNumb          = new StringCustomFieldRef();
                                    FoodicsNumb.scriptId = "custbody_da_foodics_number";
                                    FoodicsNumb.value    = invoiceobj.Number.ToString();

                                    CustomFieldRef[] customFieldRefArray = new CustomFieldRef[5];
                                    customFieldRefArray[0] = trans_id;
                                    customFieldRefArray[1] = payPercent;
                                    customFieldRefArray[2] = PayPercentAmount;
                                    customFieldRefArray[3] = FoodicsRef;
                                    customFieldRefArray[4] = FoodicsNumb;

                                    cp.customFieldList = customFieldRefArray;
                                    #endregion

                                    #region Apply Invoice
                                    // Invoice
                                    payApply                 = new CustomerPaymentApply();
                                    payApply.apply           = true;
                                    payApply.docSpecified    = true;
                                    payApply.amountSpecified = true;
                                    payApply.currency        = currency.internalId;
                                    payApply.type            = "Invoice";
                                    payApply.doc             = invoiceobj.Netsuite_Id;
                                    payApply.total           = payobj.Amount;
                                    payApply.amount          = payobj.Amount;
                                    payApply.applyDate       = payobj.Business_Date;

                                    payApplylst.Add(payApply);
                                    #endregion

                                    #region Invoice Custom Attributes



                                    #endregion

                                    // payment method
                                    if (payobj.Payment_Method_Id > 0)
                                    {
                                        is_valid = true;

                                        //payment method
                                        RecordRef payment_method = new RecordRef();
                                        payment_method.internalId = payobj.Payment_Method_Id.ToString();
                                        payment_method.type       = RecordType.customerPayment;
                                        cp.paymentMethod          = payment_method;
                                        cp.authCode = payobj.Ref.Length > 30 ? payobj.Ref.Substring(0, 30) : payobj.Ref;


                                        // amount
                                        cp.payment          = payobj.Amount;
                                        cp.paymentSpecified = true;
                                    }

                                    AplyList.apply = payApplylst.ToArray();
                                    cp.applyList   = AplyList;

                                    #endregion

                                    if (is_valid)
                                    {
                                        cps.Add(cp);
                                    }
                                    #endregion
                                }
                            }
                            catch (Exception ex)
                            {
                                LogDAO.Integration_Exception(LogIntegrationType.Error, this.GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name, "Error " + ex.Message);
                            }
                            if (cps.Count > 0)
                            {
                                WriteResponseList wr = Service(true).addList(cps.ToArray());
                                bool result          = wr.status.isSuccess;

                                UpdatedLst(invoiceMethodLst, wr);

                                // return result;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogDAO.Integration_Exception(LogIntegrationType.Error, this.GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name, "Error " + ex.Message);
                    }
                }
            }
            return(0);
        }
        public override Int64 Set(string parametersArr)
        {
            try
            {
                bool result = true;
                new CustomDAO().InvoiceRelatedUpdate();
                new CustomDAO().SetItemClass();
                #region Check Correct Invoices

                /*
                 * SELECT      *
                 * FROM            Invoice
                 * WHERE
                 * --(Location_Id = 201) and
                 * Order_Status=4
                 * and Net_Payable + Total_Discount != (select sum( Quantity  * amount) - sum(Line_Discount_Amount) from InvoiceItem where InvoiceItem.Invoice_Id =Invoice.Id and ISNULL(Item_Id,0)>0 and ProductStatus=3 )
                 *
                 * SELECT      *
                 * FROM            Invoice
                 * WHERE
                 *
                 * Order_Status=5
                 * and Net_Payable + Total_Discount != (select sum( Quantity  * amount) - sum(Line_Discount_Amount) from InvoiceItem where InvoiceItem.Invoice_Id =Invoice.Id and ISNULL(Item_Id,0)>0 and ProductStatus=6 )
                 * select * from InvoiceItem where isnull([Item_Id], 0)=0
                 *
                 * Declare @id nvarchar(50)
                 * set @id='007f91e2-a8ad-4dab-a20a-70c90e8db6ce'
                 *              select * from Invoice
                 *              where Foodics_Id =@id
                 *              select Net_Payable,Net_Payable-((Net_Payable*15)/100) from Invoice
                 *              where Foodics_Id =@id
                 *              select * from InvoiceItem
                 *              where Foodics_Id =@id
                 */
                #endregion
                List <Foodics.NetSuite.Shared.Model.Invoice> lstitemsAll = new CustomDAO().SelectInvoice(4);
                int Exe_length = 200;
                int lstend     = Exe_length;
                if (lstitemsAll.Count > 0)
                {
                    for (int Index = 0; Index < lstitemsAll.Count; Index += Exe_length)
                    {
                        if (Index + Exe_length >= lstitemsAll.Count)
                        {
                            lstend = lstitemsAll.Count - Index;
                        }
                        List <Foodics.NetSuite.Shared.Model.Invoice> invoiceLst = lstitemsAll.GetRange(Index, lstend);

                        if (invoiceLst.Count > 0)
                        {
                            #region variables
                            Invoice[] InvoiceArr = new Invoice[invoiceLst.Count];



                            Foodics.NetSuite.Shared.Model.Invoice     invoice_info;
                            Foodics.NetSuite.Shared.Model.InvoiceItem itemDetails;


                            DateTime        invoice_date;
                            Invoice         invoiceObject;
                            InvoiceItem     invoiceItemObject;
                            InvoiceItemList items;

                            RecordRef            subsid, currency, entity, location;
                            StringCustomFieldRef FoodicsRef, FoodicsNumb, CreatedBy, Source, orderDiscount;
                            CustomFieldRef[]     customFieldRefArray;
                            #endregion
                            for (int i = 0; i < invoiceLst.Count; i++)
                            {
                                try
                                {
                                    List <Foodics.NetSuite.Shared.Model.InvoiceItem> itemLst = new List <Foodics.NetSuite.Shared.Model.InvoiceItem>();
                                    List <InvoiceItem> invoiceItems = new List <InvoiceItem>();
                                    invoice_info = invoiceLst[i];
                                    Setting objSetting = new GenericeDAO <Setting>().GetWhere("Subsidiary_Netsuite_Id=" + invoice_info.Subsidiary_Id).FirstOrDefault();
                                    invoiceObject = new Invoice();
                                    #region invoice items
                                    itemLst = new GenericeDAO <Foodics.NetSuite.Shared.Model.InvoiceItem>().GetWhere(" ProductStatus =3 and Invoice_Id =" + invoice_info.Id + " and isnull(Item_Id,0) >0 ");
                                    try
                                    {
                                        for (int k = 0; k < itemLst.Count; k++)
                                        {
                                            itemDetails       = itemLst[k];
                                            invoiceItemObject = CreateInvoiceItem(objSetting, itemDetails);
                                            invoiceItems.Add(invoiceItemObject);
                                            if (itemDetails.Line_Discount_Amount > 0)
                                            {
                                                float Discount = itemDetails.Line_Discount_Amount;
                                                Foodics.NetSuite.Shared.Model.InvoiceItem OtherCharge = new Foodics.NetSuite.Shared.Model.InvoiceItem();
                                                OtherCharge.Item_Id   = objSetting.OtherChargeItem_Netsuite_Id;
                                                OtherCharge.Amount    = Discount * -1;
                                                OtherCharge.Quantity  = 1;
                                                OtherCharge.Item_Type = nameof(Item_Type.OtherChargeSaleItem);
                                                invoiceItemObject     = CreateInvoiceItem(objSetting, OtherCharge);
                                                invoiceItems.Add(invoiceItemObject);
                                            }
                                        }
                                        //if (invoice_info.Total_Discount > 0)
                                        //{
                                        //    float Discount = invoice_info.Total_Discount;
                                        //    Foodics.NetSuite.Shared.Model.InvoiceItem discountItem = new Foodics.NetSuite.Shared.Model.InvoiceItem();
                                        //    if (invoice_info.Discount_Id > 0)
                                        //        discountItem.Item_Id = invoice_info.Discount_Id;
                                        //    else
                                        //        discountItem.Item_Id = objSetting.DiscountItem_Netsuite_Id;

                                        //    discountItem.Amount = Discount * -1;
                                        //    //discountItem.Quantity = 1;
                                        //    discountItem.Item_Type = nameof(Item_Type.DiscountItem);
                                        //    invoiceItemObject = CreateInvoiceItem(objSetting, discountItem);
                                        //    invoiceItems.Add(invoiceItemObject);
                                        //}
                                    }
                                    catch (Exception ex)
                                    {
                                        LogDAO.Integration_Exception(LogIntegrationType.Error, this.GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name, "Error " + ex.Message);
                                    }
                                    //Assign invoive items
                                    items                  = new InvoiceItemList();
                                    items.item             = invoiceItems.ToArray();
                                    invoiceObject.itemList = items;

                                    //GiftCertRedemption
                                    #endregion


                                    #region Standard Attributes
                                    invoice_date = TimeZoneInfo.ConvertTimeToUtc(invoice_info.Date, TimeZoneInfo.Local);

                                    //invoiceObject.externalId = invoice_info.BarCode;
                                    invoiceObject.tranDateSpecified = true;
                                    invoiceObject.dueDateSpecified  = true;
                                    invoiceObject.tranDate          = invoice_date;
                                    invoiceObject.dueDate           = invoice_date;
                                    invoiceObject.exchangeRate      = invoice_info.Exchange_Rate;


                                    //invoiceObject.memo = invoice_info.Notes;
                                    if (invoice_info.Subsidiary_Id > 0)
                                    {
                                        subsid                   = new RecordRef();
                                        subsid.internalId        = objSetting.Subsidiary_Netsuite_Id.ToString();
                                        subsid.type              = RecordType.subsidiary;
                                        invoiceObject.subsidiary = subsid;
                                    }
                                    currency               = new RecordRef();
                                    currency.internalId    = objSetting.Currency_Netsuite_Id.ToString();
                                    currency.type          = RecordType.currency;
                                    invoiceObject.currency = currency;
                                    entity               = new RecordRef();
                                    entity.internalId    = invoice_info.Customer_Netsuite_Id > 0 ? invoice_info.Customer_Netsuite_Id.ToString() : objSetting.Customer_Netsuite_Id.ToString();
                                    entity.type          = RecordType.customer;
                                    invoiceObject.entity = entity;

                                    location               = new RecordRef();
                                    location.internalId    = invoice_info.Location_Id.ToString(); //objSetting.Location_Netsuite_Id.ToString();
                                    location.type          = RecordType.location;
                                    invoiceObject.location = location;

                                    if (invoice_info.Sales_Rep_Id > 0)
                                    {
                                        RecordRef sales_rep = new RecordRef();
                                        sales_rep.internalId   = invoice_info.Sales_Rep_Id.ToString();
                                        sales_rep.type         = RecordType.employee;
                                        invoiceObject.salesRep = sales_rep;
                                    }
                                    #endregion

                                    #region Discount
                                    if (invoice_info.Total_Discount > 0)
                                    {
                                        RecordRef discountitem = new RecordRef();
                                        discountitem.type          = RecordType.discountItem;
                                        invoiceObject.discountItem = discountitem;
                                        float taxRate = 1 + (objSetting.TaxRate / 100);
                                        if (objSetting.TaxAlwaysAppliedInItems)
                                        {
                                            invoiceObject.discountRate = (Math.Round((invoice_info.Total_Discount / taxRate), 3) * -1).ToString();
                                        }
                                        else
                                        {
                                            //if (objSetting.ItemTaxInclusive)
                                            //    invoiceObject.discountRate = (Math.Round((invoice_info.Total_Discount / taxRate), 3) * -1).ToString();
                                            //else
                                            invoiceObject.discountRate = (Math.Round(invoice_info.Total_Discount, 3) * -1).ToString();
                                        }

                                        if (invoice_info.Discount_Id > 0)
                                        {
                                            discountitem.internalId = invoice_info.Discount_Id.ToString();
                                        }
                                        else
                                        {
                                            discountitem.internalId = objSetting.DiscountItem_Netsuite_Id.ToString();
                                        }
                                    }
                                    else
                                    {
                                        invoiceObject.discountRate = "0";
                                    }
                                    orderDiscount          = new StringCustomFieldRef();
                                    orderDiscount.scriptId = "custbody_da_invoice_discount";
                                    orderDiscount.value    = invoice_info.Total_Discount.ToString();

                                    //if (invoice_info.Accounting_Discount_Item != 0)
                                    //{
                                    //    RecordRef discItem = new RecordRef();
                                    //    discItem.internalId = invoice_info.Accounting_Discount_Item.ToString();
                                    //    discItem.type = RecordType.discountItem;
                                    //    invoiceObject.discountItem = discItem;
                                    //}
                                    #endregion

                                    //RecordRef Approval = new RecordRef();
                                    //Approval.internalId = "2";
                                    ////Approval.type = RecordType.;
                                    //invoiceObject.approvalStatus = Approval;

                                    #region Invoice Custom Attributes
                                    FoodicsRef          = new StringCustomFieldRef();
                                    FoodicsRef.scriptId = "custbody_da_foodics_reference";
                                    FoodicsRef.value    = invoice_info.BarCode.ToString();

                                    FoodicsNumb          = new StringCustomFieldRef();
                                    FoodicsNumb.scriptId = "custbody_da_foodics_number";
                                    FoodicsNumb.value    = invoice_info.Number.ToString();

                                    CreatedBy          = new StringCustomFieldRef();
                                    CreatedBy.scriptId = "custbody_da_foodics_createdby";
                                    CreatedBy.value    = invoice_info.CreatedBy != null?invoice_info.CreatedBy.ToString() : "";

                                    Source          = new StringCustomFieldRef();
                                    Source.scriptId = "custbody_da_foodics_source";
                                    Source.value    = invoice_info.Source.ToString();

                                    customFieldRefArray    = new CustomFieldRef[5];
                                    customFieldRefArray[0] = orderDiscount;
                                    customFieldRefArray[1] = FoodicsRef;
                                    customFieldRefArray[2] = FoodicsNumb;
                                    customFieldRefArray[3] = CreatedBy;
                                    customFieldRefArray[4] = Source;

                                    invoiceObject.customFieldList = customFieldRefArray;
                                    #endregion
                                    InvoiceArr[i] = invoiceObject;
                                }
                                catch (Exception ex)
                                {
                                    //invoiceLst.RemoveAt(i);
                                    LogDAO.Integration_Exception(LogIntegrationType.Error, this.GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name, "Error " + ex.Message);
                                }
                            }
                            // Send invoice list to netsuite
                            WriteResponseList wr = Service(true).addList(InvoiceArr);
                            result = wr.status.isSuccess;
                            if (result)
                            {
                                //Update database with returned Netsuite ids
                                UpdatedInvoice(invoiceLst, wr);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogDAO.Integration_Exception(LogIntegrationType.Error, this.GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name, "Error " + ex.Message);
            }



            return(0);
        }
Ejemplo n.º 3
0
        public override Int64 Set(string parametersArr)
        {
            List <Foodics.NetSuite.Shared.Model.PaymentMethodEntity> lstitemsAll = new CustomDAO().SelectCustomerPayment(5).Take(2000).ToList();
            int Exe_length = 200;
            int lstend     = Exe_length;

            if (lstitemsAll.Count > 0)
            {
                for (int Index = 0; Index < lstitemsAll.Count; Index += Exe_length)
                {
                    if (Index + Exe_length >= lstitemsAll.Count)
                    {
                        lstend = lstitemsAll.Count - Index;
                    }
                    List <Foodics.NetSuite.Shared.Model.PaymentMethodEntity> returnList = lstitemsAll.GetRange(Index, lstend);
                    try
                    {
                        if (returnList.Count > 0)
                        {
                            CustomerRefund[] memoList = new CustomerRefund[returnList.Count];
                            for (int i = 0; i < returnList.Count; i++)
                            {
                                PaymentMethodEntity     invoiceReturn            = returnList[i];
                                CustomerRefund          memo                     = new CustomerRefund();
                                CustomerRefundApply[]   payApply                 = new CustomerRefundApply[1];
                                CustomerRefundApplyList AplyList                 = new CustomerRefundApplyList();
                                Foodics.NetSuite.Shared.Model.Invoice invoiceobj = new GenericeDAO <Foodics.NetSuite.Shared.Model.Invoice>().GetWhere(" Foodics_Id = '" + invoiceReturn.Foodics_Id + "'").FirstOrDefault();
                                Setting objSetting = new GenericeDAO <Setting>().GetWhere("Subsidiary_Netsuite_Id=" + invoiceobj.Subsidiary_Id).FirstOrDefault();
                                //Customer

                                RecordRef entity = new RecordRef();
                                entity.internalId = invoiceobj.Customer_Netsuite_Id > 0 ? invoiceobj.Customer_Netsuite_Id.ToString() : objSetting.Customer_Netsuite_Id.ToString();
                                entity.type       = RecordType.customer;
                                memo.customer     = entity;

                                //currency
                                RecordRef currency = new RecordRef();
                                currency.internalId = objSetting.Currency_Netsuite_Id.ToString();
                                currency.type       = RecordType.currency;
                                memo.currency       = currency;

                                //date
                                memo.tranDateSpecified = true;
                                memo.tranDate          = TimeZoneInfo.ConvertTimeToUtc(invoiceobj.Date, TimeZoneInfo.Local);

                                //exchange rate
                                memo.exchangeRate = invoiceobj.Exchange_Rate;

                                //subsidary
                                RecordRef subsid = new RecordRef();
                                subsid.internalId = invoiceobj.Subsidiary_Id.ToString();
                                subsid.type       = RecordType.subsidiary;
                                memo.subsidiary   = subsid;

                                //cp.location
                                RecordRef location = new RecordRef();
                                location.internalId = invoiceobj.Location_Id.ToString();
                                location.type       = RecordType.location;
                                memo.location       = location;

                                #region Apply Invoice
                                // Invoice
                                payApply[0]                 = new CustomerRefundApply();
                                payApply[0].apply           = true;
                                payApply[0].docSpecified    = true;
                                payApply[0].amountSpecified = true;
                                payApply[0].currency        = currency.internalId;
                                payApply[0].type            = "CreditMemo";
                                payApply[0].doc             = invoiceobj.Netsuite_Id;
                                payApply[0].total           = invoiceobj.Paid;
                                payApply[0].amount          = invoiceobj.Paid;
                                payApply[0].applyDate       = invoiceobj.Date;

                                AplyList.apply = payApply;
                                memo.applyList = AplyList;
                                #endregion

                                #region payment Method
                                RecordRef payment_method = new RecordRef();
                                payment_method.internalId = invoiceReturn.Payment_Method_Id.ToString();
                                payment_method.type       = RecordType.customerPayment;
                                memo.paymentMethod        = payment_method;
                                #endregion

                                memoList[i] = memo;
                            }


                            if (memoList.Length > 0)
                            {
                                WriteResponseList wr = Service(true).addList(memoList.ToArray());
                                bool result          = wr.status.isSuccess;
                                UpdatedLst(returnList, wr);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogDAO.Integration_Exception(LogIntegrationType.Error, this.GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name, "Error " + ex.Message);
                    }
                }
            }
            return(0);
        }