分别提交发票数量记录和数量发票

时间:2019-01-14 09:27:22

标签: php mysql arrays xml dom

我有问题。我想按指定时间以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>

如果您不了解:

在“发票”中,我只想显示我的发票。并非所有记录。发票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); }

你有什么主意吗? :)

1 个答案:

答案 0 :(得分:1)

在XML文件中创建数据之前,最好对发票进行汇总。

#!perl
use strict;
use warnings;

use CGI qw(url);

$ENV{HTTPS} = 'ON';
print url();

更新: 我已经在上面添加了$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 =