Advertisement
Guest User

Untitled

a guest
Nov 20th, 2014
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.08 KB | None | 0 0
  1. #!/usr/bin/php -q
  2. <?Php
  3.  
  4.  
  5.  
  6. require ("./holidays.php");
  7.  
  8. // global data
  9. $direct_debit_date=15;          // will require the debit for this date - or first working day after
  10. $msgid_stub="MY COMPANY ";      // message id - the reference will be added
  11. $dd_name="MY COMPANY SAS";
  12. $dd_iban="FR7600000000000000000000000";
  13. $dd_bic="BNKRRRRRRR";
  14. $dd_iddd="FR45ZZZ0000000";
  15. $dd_pmtinf="YOUR SERVICE PAYMENT ";
  16. $dd_rmtinf="INVOICE ";
  17.  
  18.  
  19.  
  20. // you may want to review $detailqry definition later in the code, to define which invoices you want to select
  21.  
  22.  
  23.  
  24. // invoice period in french
  25. $nmmonth = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
  26. $nmmois = array('Janvier', 'Fevrier', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Aout', 'Septembre', 'Octobre', 'Novembre', 'Decembre');
  27. $invper = str_replace($nmmonth, $nmmois, date("F Y"));
  28.  
  29. // tab genertor
  30. $ntab=0;
  31. function tab($dir) {
  32.         global $ntab;
  33.         $ntab+=$dir;
  34.         $t="\n";
  35.         $i=0;
  36.         while ($i++<$ntab) $t.="\t";
  37.         return $t;
  38. }
  39.  
  40. function sanitize ($str) {
  41.         return preg_replace( '/[^a-zA-Z0-9 \/-?:().,\'+s]/', '', $str );
  42. }
  43.  
  44.  
  45. //
  46. // Calcualte collection date - first working day after the 15th
  47. //
  48. $ym=date('Y-m');
  49. $reqddate=$ym."-".$direct_debit_date;
  50. while (isHoliday(strtotime($reqddate))) {
  51.         $direct_debit_date++;
  52.         $reqddate=$ym."-".$direct_debit_date;
  53. }
  54.  
  55. echo "file name : ddsepa-".date('Y-m-d').".xml\n";
  56. echo "Direct Debit Required date: $reqddate \n";
  57.  
  58. //
  59. // db connection
  60. //
  61. if ( $_ENV['HOSTNAME'] == "pollux"  ) {
  62.         echo "******\n";
  63.         echo "****** DIRECT DEBIT runnning on POLLUX\n";
  64.         echo "******\n\n";
  65.         $dbname="a2bprod";$dbuser="root";$dbpass="orion451";
  66.         $realhost=1;
  67. } else {
  68.         $dbname="a2b141";$dbuser="root";$dbpass="orion001";
  69.         $realhost=0;
  70. }
  71.  
  72. if ( !($db=mysql_connect("localhost",$dbuser, $dbpass)) ) { die ("Cannot connect to mysql..." . mysql_error());}
  73. if ( !(mysql_select_db($dbname,$db)) ) { die ("Cannot select db.." . mysql_error());}
  74.  
  75.  
  76. //
  77. // compute unique reference
  78. //
  79. $qry="select valeur from direct_debit_data where cle='lastref' ;";
  80. $res=mysql_query($qry, $db); if ( $res === FALSE) { echo $qry."\n";die(mysql_error()); }
  81. $row=mysql_fetch_row($res);
  82. $refid=$row[0]+1;
  83. $qry="update direct_debit_data set valeur='".$refid."'where cle='lastref' ; ";
  84. $res=mysql_query($qry, $db); if ( $res === FALSE) { echo $qry."\n";die(mysql_error()); }
  85.  
  86. echo "Reference for this file: $refid \n";
  87.  
  88.  
  89. // check if dd already run
  90. $qry="select valeur from direct_debit_data where cle='lastdaterun' ;";
  91. $res=mysql_query($qry, $db); if ( $res === FALSE) { echo $qry."\n";die(mysql_error()); }
  92. $row=mysql_fetch_row($res);
  93. if (substr($row[0],0,7) == date('Y-m')) {
  94.         echo "=== direct debit has already run this month (".$row[0].")- process anyway ? y/N ";
  95.         $s=fgets(STDIN);
  96.         if (strtolower(substr($s,0,1)) != "y") {
  97.                 mysql_close($db);
  98.                 die ("aborted.....\n");
  99.         }
  100. }
  101.  
  102.  
  103.  
  104. // retrieve totals to be debited
  105. //
  106. //  a detailed query for each row, and a summary for the headers
  107. //
  108. $detailqry ='select iv.id_card, min(reference), max(reference), count(*), sum(iv.open_amount) as opn, min(validfrom), min(rum) as rum, min(bic),min(iban),  ';
  109. $detailqry.=" min(if(ca.company_name = '', ca.lastname, ca.company_name) ) ";
  110. $detailqry.='from cc_invoice iv, direct_debit dd, cc_card ca ';
  111. $detailqry.='where iv.id_card = dd.id_card and iv.date >=dd.validfrom and ca.id=iv.id_card ';
  112. $detailqry.='and open_amount > 0 ';
  113. $detailqry.='and iv.description="Facture POSTPAID"  and isactive > 0 ';
  114. // the first clause will be added here to $detrailqry in the processing loop
  115. $detailqrygrp='group by 1 ';
  116.  
  117.  
  118. // ** ALL ** the transactions
  119.  
  120. $sumqry= "select count(*), sum(opn) from ( " . $detailqry . $detailqrygrp . " ) as tot; ";
  121.  
  122. $postqry ='select iv.id, iv.reference, iv.open_amount, iv.id_card from cc_invoice iv, direct_debit dd ';
  123. $postqry.='where iv.id_card = dd.id_card and iv.date >=dd.validfrom  ';
  124. $postqry.='and open_amount > 0 and iv.description="Facture POSTPAID"   and isactive > 0  ';
  125.  
  126. $restot=mysql_query($sumqry, $db); if ( $restot === FALSE) { echo $sumqry."\n";die(mysql_error()); }
  127. $rowtot=mysql_fetch_row($restot);
  128.  
  129. $uptfrstqry="update direct_debit set isfirst=0 where isactive>0 and isfirst=1 ; ";
  130.  
  131. #echo $detailqry . "\n";
  132. #echo $sumqry ."\n";
  133.  
  134.  
  135. $f=fopen("ddsepa-".date('Y-m-d').".xml", "w");
  136.  
  137. fputs($f,'<?xml version="1.0" encoding="utf-8" ?>'."\n");
  138. fputs($f,'<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02">'."\n");
  139. fputs($f,'<CstmrDrctDbtInitn>'."\n");
  140.  
  141. // group
  142. fputs ($f, "<GrpHdr>\n"); $t=tab(1);
  143.  
  144. fputs ($f, "$t<MsgId>$msgid_stub $refid</MsgId>");
  145.  
  146. fputs ($f, "$t<CreDtTm>".date ("Y-m-d\TH:i:s")."</CreDtTm>");
  147. fputs ($f, "$t<NbOfTxs>".$rowtot[0]."</NbOfTxs>");
  148. fprintf ($f, "$t<CtrlSum>%.2f</CtrlSum>", $rowtot[1]);
  149.  
  150. $xactcnt=$rowtot[0];
  151. $xactsum=$rowtot[1];
  152.  
  153. fputs($f, "$t<InitgPty>"); $t=tab(1);
  154. fputs($f, "$t<Nm>$dd_name</Nm>");
  155. //fputs($f, "$t<PstlAdr>"); $t=tab(1);
  156. //fputs($f, "$t<StrtNm>6 rue des fleurs</StrtNm>");
  157. //fputs($f, "$t<PstCd>75014</PstCd>");
  158. //fputs($f, "$t<TwnNm>Paris</TwnNm>");
  159. //fputs($f, "$t<Ctry>FR</Ctry>");
  160. //$t=tab(-1);
  161. //fputs($f, "$t</PstlAdr>");
  162. //fputs($f, "$t<CtctDtls>"); $t=tab(1);
  163. //fputs($f, "$t<Nm>john doe</Nm>");
  164. //fputs($f, "$t<EmailAdr>john@doe.org</EmailAdr>");
  165. //$t=tab(-1);
  166. //fputs($f, "$t</CtctDtls>");
  167. $t=tab(-1);
  168. fputs($f, "$t</InitgPty>");
  169. $t=tab(-1);
  170. fputs ($f, "$t</GrpHdr>\n");
  171.  
  172.  
  173. //
  174. // a bit bulky, but lets process the **first** transactions
  175. //
  176.  
  177. $first=1;
  178.  
  179. while ($first >= 0 ){
  180.  
  181.         $resdet=mysql_query($detailqry . " and isfirst=$first " . $detailqrygrp, $db); if ( $restot === FALSE) { echo $sumqry."\n";die(mysql_error()); }
  182.  
  183.         if (mysql_num_rows($resdet) > 0) {
  184.  
  185.                 // get total for this block
  186.                 $loopqry= "select count(*), sum(opn) from ( " . $detailqry . " and isfirst=$first " . $detailqrygrp ." ) as tot; ";
  187.                 $resloop=mysql_query($loopqry, $db); if ( $resloop === FALSE) { echo $loopqry."\n";die(mysql_error()); }
  188.                 $rowloop=mysql_fetch_row($resloop);
  189.  
  190.                 //
  191.                 // insert a payment info block
  192.                 //
  193.  
  194.                 fputs ($f, "$t<PmtInf>"); $t=tab(1);
  195.  
  196.                 fputs ($f, "$t<PmtInfId>$dd_pmtinf $invper</PmtInfId>");
  197.                 fputs ($f, "$t<PmtMtd>DD</PmtMtd>");
  198.                 fputs ($f, "$t<BtchBookg>false</BtchBookg>");
  199.                 fputs ($f, "$t<NbOfTxs>".$rowloop[0]."</NbOfTxs>");
  200.                 fprintf ($f, "$t<CtrlSum>%.2f</CtrlSum>", $rowloop[1]);
  201.  
  202.                 // all these efforts hust to have frst here....
  203.                 if ($first ==1) {
  204.                         fputs ($f, "$t<PmtTpInf><SvcLvl><Cd>SEPA</Cd></SvcLvl><LclInstrm><Cd>CORE</Cd></LclInstrm><SeqTp>FRST</SeqTp></PmtTpInf>");
  205.                 } else {
  206.                         fputs ($f, "$t<PmtTpInf><SvcLvl><Cd>SEPA</Cd></SvcLvl><LclInstrm><Cd>CORE</Cd></LclInstrm><SeqTp>RCUR</SeqTp></PmtTpInf>");
  207.                 }
  208.                 fputs ($f, "$t<ReqdColltnDt>".$reqddate."</ReqdColltnDt>");
  209.  
  210.                 fputs ($f, "$t<Cdtr>"); $t=tab(1);
  211.                 fputs($f, "$t<Nm>$dd_name</Nm>");
  212.                 //fputs($f, "$t<PstlAdr>"); $t=tab(1);
  213.                 //fputs($f, "$t<StrtNm>6 rue des fleurs</StrtNm>");
  214.                 //fputs($f, "$t<PstCd>75014</PstCd>");
  215.                 //fputs($f, "$t<TwnNm>Paris</TwnNm>");
  216.                 //fputs($f, "$t<Ctry>FR</Ctry>");
  217.                 //$t=tab(-1);
  218.                 //fputs($f, "$t</PstlAdr>");
  219.                 $t=tab(-1);
  220.                 fputs ($f, "$t</Cdtr>");
  221.  
  222.                 fputs ($f, "$t<CdtrAcct>"); $t=tab(1);
  223.                 fputs ($f, "$t<Id><IBAN>$dd_iban</IBAN></Id>");
  224.                 $t=tab(-1);
  225.                 fputs ($f, "$t</CdtrAcct>");
  226.                 fputs ($f, "$t<CdtrAgt>");$t=tab(1);
  227.                 fputs ($f, "$t<FinInstnId><BIC>$dd_bic</BIC></FinInstnId>");
  228.                 $t=tab(-1);
  229.                 fputs ($f, "$t</CdtrAgt>");
  230.  
  231.                 fputs ($f, "$t<ChrgBr>SLEV</ChrgBr>");
  232.                 fputs ($f, "$t<CdtrSchmeId>");$t=tab(1);
  233.                 fputs ($f, "$t<Nm>$dd_name</Nm>");
  234.                 fputs ($f, "$t<Id><PrvtId><Othr><Id>$dd_iddd</Id>");
  235.                 fputs ($f, "$t<SchmeNm><Prtry>SEPA</Prtry></SchmeNm>");
  236.                 fputs ($f, "$t</Othr></PrvtId></Id>");
  237.                 $t=tab(-1);
  238.                 fputs ($f, "$t</CdtrSchmeId>");
  239.  
  240.  
  241.                 ///// loop through all the payments
  242.  
  243.                 while ($rd=mysql_fetch_row($resdet) ) {
  244.  
  245.                         // if multiple invoices to be debited at once, makes up a ref with 20140000first-last
  246.                         if ($rd[3] > 1) {
  247.                                 $payid=$rd[1]."-".substr($rd[2],-3);
  248.                         } else {
  249.                                 $payid=$rd[1];
  250.                         }
  251.  
  252.                         fputs ($f, "$t<DrctDbtTxInf>"); $t=tab(1);
  253.  
  254.                         fputs ($f, "$t<PmtId>");$t=tab(1);
  255.                         fputs ($f, "$t<EndToEndId>".$payid."</EndToEndId>");
  256.                         $t=tab(-1);
  257.                         fputs ($f, "$t</PmtId>");
  258.  
  259.                         fprintf ($f, "$t<InstdAmt Ccy=\"EUR\">%.2f</InstdAmt>",$rd[4]);
  260.  
  261.                         fputs ($f, "$t<DrctDbtTx>"); $t=tab(1);
  262.                         fputs ($f, "$t<MndtRltdInf>"); $t=tab(1);
  263.                         fputs ($f, "$t<MndtId>".$rd[6]."</MndtId>");
  264.                         fputs ($f, "$t<DtOfSgntr>".substr($rd[5],0,10)."</DtOfSgntr>");
  265.                         $t=tab(-1);
  266.                         fputs ($f, "$t</MndtRltdInf>");
  267.                         $t=tab(-1);
  268.                         fputs ($f, "$t</DrctDbtTx>");
  269.  
  270.  
  271.                         fputs ($f, "$t<DbtrAgt>");$t=tab(1);
  272.                         fputs ($f, "$t<FinInstnId>");$t=tab(1);
  273.                         fputs ($f, "$t<BIC>".$rd[7]."</BIC>");
  274.                         $t=tab(-1);
  275.                         fputs ($f, "$t</FinInstnId>");
  276.                         $t=tab(-1);
  277.                         fputs ($f, "$t</DbtrAgt>");
  278.                         fputs ($f, "$t<Dbtr>");$t=tab(1);
  279.                         fputs ($f, "$t<Nm>".sanitize($rd[9])."</Nm>");
  280.                         $t=tab(-1);
  281.                         fputs ($f, "$t</Dbtr>");
  282.                         fputs ($f, "$t<DbtrAcct>");$t=tab(1);
  283.                         fputs ($f, "$t<Id><IBAN>".$rd[8]."</IBAN></Id>");
  284.                         $t=tab(-1);
  285.                         fputs ($f, "$t</DbtrAcct>");
  286.  
  287.                         fputs ($f, "$t<RmtInf><Ustrd>$dd_rmtinf". $invper . " " .$payid."</Ustrd></RmtInf>");
  288.  
  289.                         $t=tab(-1);
  290.                         fputs ($f, "$t</DrctDbtTxInf>");
  291.  
  292.                 }
  293.  
  294.                 $t=tab(-1);
  295.                 fputs ($f, "$t</PmtInf>");
  296.         }
  297.  
  298.         // switches from first to recur
  299.         $first--;
  300. }
  301. fputs ($f, "$t</CstmrDrctDbtInitn>");
  302. fputs ($f, "$t</Document>\n");
  303.  
  304.  
  305. echo "\n\nDone\n\nNumber of transactions in file: $xactcnt\n";
  306. printf ("Amount in file: %.2f eur\n\n", $xactsum);
  307.  
  308. echo "update the database ? y/N ";
  309. $s=fgets(STDIN);
  310. if (strtolower(substr($s,0,1)) != "y") {
  311.         mysql_close($db);
  312.         die ("aborted.....\n");
  313. }
  314. echo "now updating database..\n";
  315.  
  316. // update all dd entries to not FIRST as they've been processed !
  317. $ru=mysql_query($uptfrstqry); if ($ru === FALSE) { echo $uptfrstqry ."\n";die(mysql_error()); }
  318.  
  319. $totposted=0;
  320. $totxact=0;
  321.  
  322. $res=mysql_query($postqry, $db); if ( $res === FALSE) { echo $postqry."\n";die(mysql_error()); }
  323. while ($row=mysql_fetch_row($res) ) {
  324.  
  325.         $totposted += $row[2];
  326.         $totxact++;
  327.  
  328.         $insq ="insert into cc_logpayment ( `date`, payment, card_id, id_logrefill, description, added_refill, payment_type, added_commission  ) ";
  329.         $insq.="values ('".$reqddate." 01:00:00', ".$row[2].", ".$row[3].", null, 'DD-".$refid."', 0,0,0) ; ";
  330.  
  331.         $ri=mysql_query($insq); if ( $ri === FALSE) { echo $insq."\n";die(mysql_error()); }
  332.  
  333.         $insq="insert into cc_invoice_payment ( id_invoice, id_payment ) values ( ".$row[0]." , LAST_INSERT_ID() ) ";
  334.         $ri=mysql_query($insq); if ( $ri === FALSE) { echo $insq."\n";die(mysql_error()); }
  335.  
  336. }
  337.  
  338. echo "Total posted in DB: $totposted \n";
  339. echo "Total transactions: $totxact \n";
  340.  
  341. if ( $totposted != $xactsum ) {
  342.         echo "\n\n";
  343.         echo "*****************************************************************\n";
  344.         echo "*****************************************************************\n";
  345.         echo "*****************************************************************\n";
  346.         echo "*****************************************************************\n";
  347.         echo "\n\n";
  348.         echo "Warning - sum from the file ($xactsum) and from the db ($totposted) posting differ.... \n\n";
  349.         echo "please check\n";
  350.  
  351. }
  352.  
  353. fclose($f);
  354. mysql_close($db);
  355.  
  356.  
  357.  
  358. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement