/** * @file * @brief class Acc_Ledger_History_Purchase , list of operations * * display or export operations in HTML , PDF or CSV */ /** * @brief Display the operations for Purchase * @see acc_ledger_historyTest.php */ class Acc_Ledger_History_Purchase extends Acc_Ledger_History { private $data;//!< Contains rows from SQL /** * @param mixed $data */ public function set_data($data) { //!< Contains rows from SQL $this->data = $data; return $this; } public function __construct(\Database $cn, $pa_ledger, $p_from, $p_to, $p_mode) { parent::__construct($cn, $pa_ledger, $p_from, $p_to, $p_mode); $this->filter_operation='all'; $this->ledger_type='ACH'; } public function get_filter_operation() { return $this->filter_operation; } /** * @brief display the accounting */ public function export_accounting_html() { $ledger_history=new Acc_Ledger_History_Generic($this->db, $this->ma_ledger, $this->m_from, $this->m_to, $this->m_mode); $ledger_history->export_accounting_html(); } public function export_detail_html() { $own=new Noalyss_Parameter_Folder($this->db); $this->get_row(); $this->add_vat_info(); $this->prepare_reconcile_date(); include NOALYSS_TEMPLATE."/acc_ledger_history_purchase_detail.php"; } public function export_extended_html() { $this->get_row(); $this->add_vat_info(); $this->add_additional_tax_info(); $this->prepare_detail(); $this->prepare_reconcile_date(); include NOALYSS_TEMPLATE."/acc_ledger_history_purchase_extended.php"; } /** * @brief display in HTML following the mode */ function export_html() { switch ($this->m_mode) { case "E": $this->export_extended_html(); break; case "D": $this->export_detail_html(); break; case "L": $this->export_oneline_html(); break; case "A": $this->export_accounting_html(); break; default: break; } } /** * display in HTML one operation by line */ public function export_oneline_html() { $this->get_row(); $this->prepare_reconcile_date(); $nb_other_tax=$this->has_other_tax(); require_once NOALYSS_TEMPLATE.'/acc_ledger_history_purchase_oneline.php'; } /** * Get the rows from jrnx and quant* tables * @param int $p_limit max of rows to returns * @param int $p_offset the number of rows to skip */ public function get_row($p_limit=-1, $p_offset="") { $periode=sql_filter_per($this->db, $this->m_from, $this->m_to, 'p_id', 'jr_tech_per'); $cond_limite=($p_limit!=-1)?" limit ".$p_limit." offset ".$p_offset:""; $sql_filter=$this->build_filter_operation(); $ledger_list=join(",", $this->ma_ledger); $sql=" with row_purchase as (select qp_internal, qp_supplier,sum(qp_price) as novat, sum(qp_vat) as vat , sum(qp_vat_sided) as tva_sided , sum(qp_nd_amount) as noded_amount, sum(qp_nd_tva) as noded_vat, sum(qp_dep_priv) as private_amount from quant_purchase group by qp_supplier,qp_internal), supplier_detail as ( select x.f_id as f_id, (select ad_value from fiche_detail where ad_id=1 and f_id=x.f_id) as name, (select ad_value from fiche_detail where ad_id=32 and f_id=x.f_id) as first_name, (select ad_value from fiche_detail where ad_id=23 and f_id=x.f_id) as qcode from fiche as x) , row_currency as ( select sum(oc_amount) as sum_oc_amount,sum(oc_vat_amount) as sum_oc_vat_amount,jrnx.j_grpt from operation_currency join jrnx using (j_id) join quant_purchase qp using (j_id) group by j_grpt ), other_tax as (select sum(case when j_debit is true then j_montant else 0-j_montant end) other_tax_amount ,j_grpt from jrnx j1 join jrn_tax jt2 on (j1.j_id=jt2.j_id) group by j_grpt) select name, first_name, qcode, jrn.jr_id, jr_pj_number, to_char(jr_date,'DD.MM.YYYY') as str_date, to_char(jr_date,'DDMMYY') as str_date_short, to_char(jr_date_paid,'DD.MM.YYYY') as str_date_paid, jr_internal, qp_supplier, jrn.jr_comment, jr_pj_name, vat, tva_sided, novat, noded_amount, noded_vat, private_amount, novat+vat-tva_sided as tvac, n_text, jr_grpt_id, jrn.currency_id, jrn.currency_rate, jrn.currency_rate_ref, sum_oc_amount, sum_oc_vat_amount, cr_code_iso, coalesce (other_tax_amount,0) other_tax_amount from jrn join row_purchase on (qp_internal=jr_internal) join supplier_detail on (qp_supplier=f_id) left join jrn_note using (jr_id) left join row_currency as rc on (rc.j_grpt = jrn.jr_grpt_id) left join currency as c on (c.id=jrn.currency_id) left join other_tax as ot on (ot.j_grpt=jrn.jr_grpt_id) where jr_def_id in ({$ledger_list}) {$sql_filter} and {$periode} {$cond_limite} order by jrn.jr_date, substring(jr_pj_number,'[0-9]+$')::numeric "; $this->data=$this->db->get_array($sql); } /** * @brief preprare the query for fetching the detailed VAT of an operation */ private function add_vat_info() { $prepare=$this->db->is_prepare("vat_infop"); if ($prepare==FALSE) { $this->db->prepare("vat_infop", " select sum(qp_vat) vat_amount , qp_vat_code from quant_purchase where qp_internal = $1 group by qp_vat_code order by qp_vat_code"); } $nb_row=count($this->data); for ($i=0; $i<$nb_row; $i++) { $ret=$this->db->execute("vat_infop", array($this->data[$i]["jr_internal"])); $array=Database::fetch_all($ret); $this->data[$i]["detail_vat"]=$array; } } /** * Prepare the query for fetching detail of an operation */ private function prepare_detail() { if ($this->db->is_prepare("detail_purchase")==FALSE) { $this->db->prepare("detail_purchase", " with card_name as (select f_id,ad_value as name from fiche_detail where ad_id=1), card_qcode as (select f_id,ad_value as qcode from fiche_detail where ad_id=23) select qp_price,qp_quantite,qp_vat,qp_vat_code,qp_unit,qp_vat_sided,name,qcode,tva_label, qp_price+qp_vat-qp_vat_sided as tvac from quant_purchase join jrnx using (j_id) join card_name on (card_name.f_id=qp_fiche) join card_qcode on (card_qcode.f_id=qp_fiche) left join tva_rate on ( qp_vat_code=tva_id) where qp_internal=$1 "); } } /** * To get data * @return array of rows */ function get_data() { return $this->data; } /** * @brief export Purchase in CSV */ function export_csv() { // Prepare the query for reconcile date $prepared_query=new Prepared_Query($this->db); $prepared_query->prepare_reconcile_date(); $nb_other_tax=$this->has_other_tax(); $export=new Noalyss_Csv(_('journal')); $export->send_header(); $this->get_row(); $this->prepare_reconcile_date(); $this->add_vat_info(); $own=new Noalyss_Parameter_Folder($this->db); $title=array(); $title[]=_('Date'); $title[]=_("Paiement"); $title[]=_("operation"); $title[]=_("Pièce"); $title[]=_("Fournisseur"); $title[]=_("Note"); $title[]=_("interne"); $title[]=_("HTVA"); $title[]=_("privé"); $title[]=_("DNA"); $title[]=_("tva non ded."); $title[]=_("TVA NP"); if ( $own->MY_TVA_USE=='Y') { $a_Tva=$this->db->get_array("select tva_id,tva_label from tva_rate order by tva_rate,tva_label,tva_id"); foreach($a_Tva as $line_tva) { $title[]="Tva ".$line_tva['tva_label']; } } if ($nb_other_tax>0) { $title[]=_("Autre taxe"); } $title[]=_("TVAC/TTC"); $title[]=_("Devise"); $title[]=_("Devise HTVA"); $title[]=_("Devise TVA"); $title[]=_("Taux ref"); $title[]=_("Taux utilisé"); $title[]=_("Date paiement"); $title[]=_("Code paiement"); $title[]=_("Méthode paiement"); $title[]=_("Montant paiement"); $title[]=_("n° opération"); $export->write_header($title); foreach ($this->data as $line) { $export->add($line['str_date']); $export->add($line['str_date_paid']); $export->add($line['jr_id']); $export->add($line['jr_pj_number']); $export->add($line['name']." ". $line["first_name"]." ". $line["qcode"]); // qp_supplier $export->add($line['jr_comment']); $export->add($line['jr_internal']); $export->add($line['novat'],"number"); $export->add($line['private_amount'],"number"); $export->add($line['noded_amount'],"number"); $export->add($line['noded_vat'],"number"); $export->add($line['tva_sided'],"number"); $a_tva_amount=array(); if ($own->MY_TVA_USE=='Y') { //- set all TVA to 0 foreach ($a_Tva as $l) { $t_id=$l["tva_id"]; $a_tva_amount[$t_id]=0; } foreach ($line['detail_vat'] as $lineTVA) { $idx_tva=$lineTVA['qp_vat_code']; $a_tva_amount[$idx_tva]=$lineTVA['vat_amount']; } foreach ($a_Tva as $line_tva) { $a=$line_tva['tva_id']; $export->add($a_tva_amount[$a], "number"); } } if ( $nb_other_tax > 0) { $export->add($line['other_tax_amount'],"number"); } $export->add(bcadd($line['other_tax_amount'],$line['tvac'],2),"number"); /** * Add currency info */ $export->add($line['cr_code_iso']); $export->add($line['sum_oc_amount'],'number'); $export->add($line['sum_oc_vat_amount'],'number'); $export->add($line['currency_rate'],'number'); $export->add($line['currency_rate_ref'],'number'); /** * Retrieve payment if any */ $ret_reconcile=$this->db->execute('reconcile_date',array($line['jr_id'])); $max=Database::num_row($ret_reconcile); if ($max > 0) { for ($e=0;$e<$max;$e++) { $row=Database::fetch_array($ret_reconcile, $e); $export->add($row['jr_date']); $export->add($row['qcode_bank']); $export->add($row['qcode_name']); $export->add($row['jr_montant'],"number"); $export->add($row['jr_internal']); } } $export->write(); } } }