Exemplo n.º 1
0
        private OriginalInvoiceData BuildOriginalInvoiceData(DictionaryDataSet ds)
        {
            var row = Session.CompanyInfo;
            OriginalInvoiceData invoiceData = new OriginalInvoiceData();

            invoiceData.RefID = Guid.NewGuid().ToString();
            //0313686411
            invoiceData.SellerTaxCode     = "0101243150-999";
            invoiceData.SellerLegalName   = row.Name;
            invoiceData.SellerAddressLine = row.Address;
            //
            invoiceData.BuyerLegalName   = ds.Customer.Rows[0]["CustomerName"].ToString();
            invoiceData.BuyerAddressLine = ds.Customer.Rows[0]["Address"].ToString();

            invoiceData.TemplateCode      = "01GTKT0/157";
            invoiceData.InvoiceType       = "01GTKT0";
            invoiceData.InvoiceSeries     = "AB/21E";
            invoiceData.PaymentMethodName = "Tiền mặt";
            invoiceData.AdjustmentType    = 1;
            invoiceData.IsSendEmail       = false;

            invoiceData.ExchangeRate = 1;

            invoiceData.TotalAmountWithoutVAT = Decimal.Parse(ds.SAInvoice.Rows[0]["TotalAmount"].ToString());
            invoiceData.VatPercentage         = 0;
            invoiceData.TotalVATAmount        = invoiceData.TotalAmountWithoutVAT * invoiceData.VatPercentage / 100;
            invoiceData.TotalAmountWithVAT    = invoiceData.TotalAmountWithoutVAT + invoiceData.TotalVATAmount;

            int count = 1;
            List <OriginalInvoiceDetail> detail = new List <OriginalInvoiceDetail>();

            BLSAInvoice bLSAInvoice = new BLSAInvoice();
            DataTable   table       = bLSAInvoice.GetSAInvoiceDetailAll(Guid.Parse(ds.SAInvoice.Rows[0]["RefID"].ToString()));

            for (int i = 0; i < table.Rows.Count; i++)
            {
                OriginalInvoiceDetail newDetail = new OriginalInvoiceDetail();
                newDetail.LineNumber    = count;
                newDetail.ItemCode      = table.Rows[0]["InventoryItemCode"].ToString();
                newDetail.ItemName      = table.Rows[0]["InventoryItemName"].ToString();
                newDetail.UnitName      = table.Rows[0]["UnitName"].ToString();
                newDetail.Quantity      = decimal.Parse(table.Rows[0]["UnitPrice"].ToString());
                newDetail.Amount        = decimal.Parse(table.Rows[0]["SaleAmount"].ToString());;
                newDetail.VatPercentage = 0;
                newDetail.VatAmount     = 0;
                newDetail.UnitPrice     = decimal.Parse(table.Rows[0]["UnitPrice"].ToString());

                detail.Add(newDetail);
            }

            invoiceData.OriginalInvoiceDetail = detail;

            return(invoiceData);
        }
Exemplo n.º 2
0
        private void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (oBLSAInvoice.InsertUpdate(dsDictionary, 0))
                {
                    var updateOrder = oBLOrder.ChangeOrderStatus(SAInvoiceRow.OrderID, (int)EnumOrderStatus.Done);
                    if (updateOrder)
                    {
                        MessageBoxCommon.ShowMessage("Lập hóa đơn thành công");
                        DialogResult = DialogResult.OK;
                    }
                }
                try
                {
                    string   token                  = null;
                    IInvoice invoiceObject          = null;
                    var      authen                 = MeInvoiceFactory.CreatUserClass();
                    GetTokenOperationResult oResult = authen.GetToken("8C3B3E41F3D34F639CEAF489FD62AD33", "0101243150-999", "*****@*****.**", "12345678a");
                    if (oResult.Success && !string.IsNullOrWhiteSpace(oResult.Token))
                    {
                        token = oResult.Token;
                    }
                    OriginalInvoiceData        invoiceData = BuildOriginalInvoiceData(dsDictionary);
                    List <OriginalInvoiceData> lstData     = new List <OriginalInvoiceData>();
                    lstData.Add(invoiceData);
                    //Tạo dữ liệu hóa đơn định dạng XML
                    invoiceObject = MeInvoiceFactory.CreatInvoiceClass(token);
                    CreateInvoiceOperationResult result = invoiceObject.CreateEInvoice(lstData);
                    if (result.Success && result.EInvoiceResultDataList != null && result.EInvoiceResultDataList.Count > 0)
                    {
                        XmlDocument invoiceXML = new XmlDocument();
                        invoiceXML.LoadXml(result.EInvoiceResultDataList[0].InvoiceData);
                        MISA.MeInvoice.DataContract.SDKResult.SignedXmlResult CertFile = SignerUtils.SignByFile(invoiceXML.InnerXml, @"D:\MST0101243150-999-V2.p12", "12345678");
                        List <PublishInvoiceData> signedInvoice = new List <PublishInvoiceData>();
                        PublishInvoiceData        pubInvData    = new PublishInvoiceData();
                        pubInvData.RefID         = invoiceData.RefID;
                        pubInvData.TransactionID = result.EInvoiceResultDataList[0].TransactionID;
                        pubInvData.InvoiceData   = CertFile.SignedXmlContent;
                        signedInvoice.Add(pubInvData);
                        PublishInvoiceOperationResult oPubResult = invoiceObject.PublishInvoice(signedInvoice);
                        if (oPubResult.Success)
                        {
                            List <PublishInvoiceResult> pubResult = oPubResult.PublishInvoiceResultList;
                            if (pubResult != null && pubResult.Count > 0)
                            {
                                BLSAInvoice bLSAInvoice = new BLSAInvoice();

                                bLSAInvoice.UpdateInvoiceNumber(Guid.Parse(dsDictionary.SAInvoice.Rows[0]["RefID"].ToString()), pubResult[0].InvoiceNumber, pubInvData.TransactionID);
                                btnPrint.Visible = true;
                                WebClient client = new WebClient();
                                string    reply  = client.DownloadString("https://meinvoice.vn/tra-cuu/GetRequestTimeEnCode");
                                var       link   = String.Format("http://meinvoice.vn/tra-cuu/downloadhandler.ashx?type=pdf&code={0}&viewer=1&ext={1}", pubInvData.TransactionID, reply.ToString().Substring(55, 8));
                                Process.Start(link);
                            }
                        }
                    }
                }
                catch (Exception)
                {
                }
            }
            catch (Exception ex)
            {
                MessageBoxCommon.ShowException(ex);
            }
        }