using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Agrotop
{
    public class SAP 
    {
        public SAPbobsCOM.Company oCompany = null;

        public SAP()
        {
            oCompany = (new SAPbobsCOM.Company());
            
            oCompany.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_MSSQL2014;
            oCompany.DbUserName = "sa";
            oCompany.DbPassword = "Agrotop2016*";
            oCompany.Server = "192.168.11.92"; 
            oCompany.CompanyDB = "Z_PRUEBA_AVENATOP_DOLAR";
            oCompany.UserName = "camiongo";
            oCompany.Password = "roberto";
            oCompany.LicenseServer = "192.168.11.92:30000";
            oCompany.UseTrusted = false;
            oCompany.language = SAPbobsCOM.BoSuppLangs.ln_Spanish_La;
        }


        public Respuesta setFolioNumber(string DocEntry, String nuevoFolio)
        {
            Respuesta r = new Respuesta();
            SAPbobsCOM.Documents oOrder = (SAPbobsCOM.Documents)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oInvoices);
            try
            {
                if (oOrder.GetByKey(Int32.Parse(DocEntry)))
                {
                    oOrder.FolioNumber = Int32.Parse(nuevoFolio);
                    oOrder.Update();
                }
                else
                {
                    r.message = this.oCompany.GetLastErrorDescription();
                    r.code = this.oCompany.GetLastErrorCode();
                }
            }
            catch (Exception e)
            {
                r.code = -1;
                r.message = this.oCompany.GetLastErrorDescription();
            }
            return r;
        }

        public Respuesta setBusinessPartner(string CardCode,
                string CardName,
                string Phone1,
                string Email,
                string Activity,
                string RUT)
        {
            Respuesta r = new Respuesta();
            dynamic BP = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oBusinessPartners);
            try
            {
                if (BP.GetByKey(CardCode))
                {
                    BP.Notes = Activity;
                    BP.Update();
                }
                else
                {
                    BP.CardType = SAPbobsCOM.BoCardTypes.cCustomer;
                    BP.GroupCode = 100; // Default;
                    BP.VatLiable = SAPbobsCOM.BoVatStatus.vLiable;
                    BP.CardName = CardName;
                    BP.CardCode = CardCode;
                    BP.Phone1 = Phone1;
                    BP.EmailAddress = Email;
                    BP.Notes = Activity;
                    BP.PayTermsGrpCode = -1; //contado
                    
                    BP.FederalTaxID = RUT;
                    BP.Currency = "$";
                    BP.PriceListNum = 1;
                    BP.CompanyPrivate = SAPbobsCOM.BoCardCompanyTypes.cCompany;
                    BP.Website = "AUTOMATICO VIA SOFTWARE";
                    r.code = BP.Add();
                    r.message = "OK";
                    if (r.code != 0)
                    {
                        r.message = oCompany.GetLastErrorDescription();
                    }
                    
                    
                }
            } catch (Exception e)
            {
                r.code = 0;
                r.message = this.oCompany.GetLastErrorDescription();
            }
            return r;
        }

        public Respuesta createDelivery()
        {
            Respuesta r = new Respuesta();

            return r;

        }

        public Respuesta createOrder(string CardCode, int PaymentCode, string Items, string OC, string delivery, string DirEntrega, string DirFactura)
        {
            Respuesta r = new Respuesta();
            SAPbobsCOM.Documents oOrder = (SAPbobsCOM.Documents)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders);
            try
            {
                oOrder.CardCode = CardCode;
                oOrder.DiscountPercent = 0;
                oOrder.SalesPersonCode = 109;
                oOrder.PaymentGroupCode = PaymentCode;
                oOrder.Comments = "SOFTWARE PROINDAR / OC " + OC + " / " + delivery;
                oOrder.Address = DirFactura; // Direccion Factura
                oOrder.Address2 = DirEntrega; // Direccion Entrega

                var due = DateTime.Today.AddMonths(1);

                oOrder.DocDueDate = due;
                oOrder.DocDate = DateTime.Today;

                string[] strArray = Items.Split('|');
                for (int i = 0; i < strArray.Length; i++)
                {
                    string[] line = strArray[i].Split('@');
                    oOrder.Lines.ItemCode = line[0];
                    oOrder.Lines.Quantity = double.Parse(line[1]);
                    oOrder.Lines.Price = double.Parse(line[2]);
                    oOrder.Lines.TaxCode = "IVA";
                    oOrder.Lines.Add();
                }

                r.code = oOrder.Add();
                r.message = oCompany.GetNewObjectKey();

                if (r.code != 0)
                {
                    throw new System.ArgumentException(oCompany.GetLastErrorDescription());
                }

            }
            catch (Exception ex)
            {
                r.code = 1;
                r.message = ex.Message;
            }
            return r;
        }
    }

}