首页 > 解决方案 > 分别提交发票和数量发票的数量记录

问题描述

我有个问题。我想在 XML 中按指定时间汇总我的发票。

看看我的 XML 此刻的样子:

<?xml version="1.0" encoding="utf-8"?>
<Header>
    <FormCode>1.0</FormCode>
    <Invoice type="G">
        <IDInvoice>001/2019</IDInvoice>
        <FullAmount>200</FullAmount>
        <CostWithTax>220</CostWithTax>
    </Invoice>
    <Invoice type="G">
        <IDInvoice>002/2019</IDInvoice>
        <FullAmount>100</FullAmount>
        <CostWithTax>110</CostWithTax>
    </Invoice>
    <Invoice type="G">
        <IDInvoice>002/2019</IDInvoice>
        <FullAmount>200</FullAmount>
        <CostWithTax>220</CostWithTax>
    </Invoice>
    <InvoiceControl>
        <QuantityInvoices>3</QuantityInvoices>
        <ValueInvoices>550</ValueInvoices>
    </InvoiceControl>
    <InvoiceRecord type="G">
        <IDInvoice>001/2019</IDInvoice>
        <TitleOfRecord>Teddy Bear</TitleOfRecord>
        <Amount>100</Amount>
        <Quantity>2</Quantity>
        <FullAmount>200</FullAmount>
        <CostWithTax>220</CostWithTax>
    </InvoiceRecord>
    <InvoiceRecord type="G">
        <IDInvoice>002/2019</IDInvoice>
        <TitleOfRecord>Teddy Bear</TitleOfRecord>
        <Amount>100</Amount>
        <Quantity>1</Quantity>
        <FullAmount>100</FullAmount>
        <CostWithTax>110</CostWithTax>
    </InvoiceRecord>
    <InvoiceRecord type="G">
        <IDInvoice>002/2019</IDInvoice>
        <TitleOfRecord>Teddy Bear XL</TitleOfRecord>
        <Amount>200</Amount>
        <Quantity>1</Quantity>
        <FullAmount>200</FullAmount>
        <CostWithTax>220</CostWithTax>
    </InvoiceRecord>
    <InvoiceRecordControl>
        <QuantityRecordsOfInvoices>3</QuantityRecordsOfInvoices>
        <ValueRecordsOfInvoices>500</ValueRecordsOfInvoices>
    </InvoiceRecordControl>
</Header>

我希望我的 XML 像这样:

<?xml version="1.0" encoding="utf-8"?>
<Header>
   <FormCode>1.0</FormCode>
    <Invoice type="G">
        <IDInvoice>001/2019</IDInvoice>
        <FullAmount>200</FullAmount>
        <CostWithTax>220</CostWithTax>
    </Invoice>
    <Invoice type="G">
        <IDInvoice>002/2019</IDInvoice>
        <FullAmount>300</FullAmount>
        <CostWithTax>330</CostWithTax>
    </Invoice>
    <InvoiceControl>
        <QuantityInvoices>2</QuantityInvoices>
        <ValueInvoices>550</ValueInvoices>
    </InvoiceControl>
    <InvoiceRecord type="G">
        <IDInvoice>001/2019</IDInvoice>
        <TitleOfRecord>Teddy Bear</TitleOfRecord>
        <Amount>100</Amount>
        <Quantity>2</Quantity>
        <FullAmount>200</FullAmount>
        <CostWithTax>220</CostWithTax>
    </InvoiceRecord>
    <InvoiceRecord type="G">
        <IDInvoice>002/2019</IDInvoice>
        <TitleOfRecord>Teddy Bear</TitleOfRecord>
        <Amount>100</Amount>
        <Quantity>1</Quantity>
        <FullAmount>100</FullAmount>
        <CostWithTax>110</CostWithTax>
    </InvoiceRecord>
    <InvoiceRecord type="G">
        <IDInvoice>002/2019</IDInvoice>
        <TitleOfRecord>Teddy Bear XL</TitleOfRecord>
        <Amount>200</Amount>
        <Quantity>1</Quantity>
        <FullAmount>200</FullAmount>
        <CostWithTax>220</CostWithTax>
    </InvoiceRecord>
    <InvoiceRecordControl>
        <QuantityRecordsOfInvoices>3</QuantityRecordsOfInvoices>
        <ValueRecordsOfInvoices>500</ValueRecordsOfInvoices>
    </InvoiceRecordControl>
</Header>

如果你不明白:

在“发票”中,我只想显示我的发票。不是所有的记录。Invoice 001/2019 在 MySQL 表中只有 1 条记录,但 002/2019 有 2 条记录。

在“InvoiceControl”中,我想汇总所有发票的数量和价值。

在“InvoiceRecord”中,我想显示发票上的所有位置。例如,泰迪熊和泰迪熊 XL。

在“InvoiceRecordControl”中,我想对我的发票上的数量记录求和,并对所有不含税的发票进行估价。

那是我的代码:

<?php

$mysqli = new mysqli("localhost", "xxx", "xxx", "xxx"); 
$array = array();

$query = "SELECT * 
               FROM FA INNER JOIN invoice_products
               ON FA.id = invoice_products.id_invoice
               WHERE FA.issue_data BETWEEN ? AND ? ";

function createXMLfile($array) {
    $dom = new DOMDocument('1.0', 'utf-8');

    $header = $dom->createElement('Header');
    $root->appendChild($header);

    for($i=0; $i<count($array); $i++) {

        $formCode = $naglowek->appendChild($dom->createElement('FormCode', '1.0') );
        $invoice = $dom->createElement('Invoice');
        $root->appendChild($invoice);

        $invoice->setAttribute('type', 'G');

        $arrayIDInvoice          = $array[$i]['id_invoice'];
        $arrayAmount             = $array[$i]['amount'];
        $arrayQuantity           = $array[$i]['quantity'];
        $TAX                     = $array[$i]['tax'];

        $fullAmountWithTax       = $$fullAmountWithTax+($arrayAmount*$arrayQuantity)*$TAX;
        $valueInvoiceTAX         = $valueInvoiceTAX+$fullAmountWithTax;

        $invoice->appendChild($dom->createElement('IDInvoice', $arrayIDInvoice) );
        $invoice->appendChild($dom->createElement('FullAmount', $arrayAmount*$arrayQuantity) );
        $invoice->appendChild($dom->createElement('CostWithTax', ($arrayAmount*$arrayQuantity)*$TAX) );
    }

        $invoiceCTRL = $dom->createElement('InvoiceControl');
        $root->appendChild($invoiceCTRL);

        $invoiceCTRL->appendChild($dom->createElement('QuantityInvoices', count($array) ) );
        $invoiceCTRL->appendChild($dom->createElement('ValueInvoices', $valueInvoiceTAX) );

    for($i=0; $i<count($array); $i++) {
        $arrayIDInvoice          = $array[$i]['id_invoice'];
        $arrayAmount             = $array[$i]['amount'];
        $arrayQuantity           = $array[$i]['quantity'];
        $arrayTitle              = $array[$i]['title'];
        $TAX                     = $array[$i]['tax'];

        $fullAmount              = $fullAmount+$arrayAmount*$arrayQuantity;
        $valueInvoice            = $valueInvoice+$fullAmount;

        $invoiceRecord = $dom->createElement('InvoiceRecord');
        $invoiceRecord->setAttribute('type', 'G');
        $root->appendChild($invoiceRecord);

        $invoiceRecord->appendChild($dom->createElement('IDInvoice', $arrayIDInvoice) );
        $invoiceRecord->appendCHild($dom->createElement('TitleOfRecord', $arrayTitle) );
        $invoiceRecord->appendChild($dom->createElement('Amount', $arrayAmount) );
        $invoiceRecord->appendChild($dom->createElement('Quantity', $arrayQuantity) );
        $invoiceRecord->appendChild($dom->createElement('FullAmount', $arrayAmount*$arrayQuantity) );
        $invoiceRecord->appendChild($dom->createElement('CostWithTax', ($arrayAmount*$arrayQuantity)*$TAX) );
    }

    $recordCTRL = $dom->createElement('InvoiceRecordControl');
    $root->appendChild($recordCTRL);

    $recordCTRL->appendChild($dom->createElement('QuantityRecordsOfInvoices', count($array) ) );
    $recordCTRL->appendChild($dom->createElement('ValueRecordsOfInvoices', $valueInvoice) );

    $dom->appendChild($root); }

你有什么主意吗?:)

标签: phpmysqlarraysxmldom

解决方案


您最好在 XML 文件中创建数据之前汇总发票。

$invoiceTotals = array();
foreach ( $array as $invoice )   {
    $amount = $invoice['amount'] * $invoice['quantity'];
    $tax = $invoice['tax'] * $invoice['quantity'];
    if ( isset ( $invoiceTotals[$invoice["id_invoice"]] ))  {
        $invoiceTotals[$invoice["id_invoice"]]['FullAmount'] += $amount;
        $invoiceTotals[$invoice["id_invoice"]]['CostWithTax'] += $tax;
    }
    else    {
        $invoiceTotals[$invoice["id_invoice"]] = ['FullAmount' => $amount, 'CostWithTax' => $tax ];
    }
}
foreach ( $invoiceTotals as $invoiceID => $total ) {
    $invoice = $dom->createElement('Invoice');
    $root->appendChild($invoice);
    $invoice->setAttribute('type', 'G');
    $invoice->appendChild($dom->createElement('IDInvoice', $invoiceID) );
    $invoice->appendChild($dom->createElement('FullAmount', $total['FullAmount']) );
    $invoice->appendChild($dom->createElement('CostWithTax',$total['CostWithTax']) );

    $valueInvoiceTAX = $valueInvoiceTAX+$total['CostWithTax'];
}

更新:我已经添加了$valueInvoiceTAX =上述内容,您应该将发票控制总计更新为

    $invoiceCTRL = $dom->createElement('InvoiceControl');
    $root->appendChild($invoiceCTRL);

    $invoiceCTRL->appendChild($dom->createElement('QuantityInvoices', count($invoiceTotals) ) );
    $invoiceCTRL->appendChild($dom->createElement('ValueInvoices', $valueInvoiceTAX) );

推荐阅读