3 require_once 'Pman/Roo.php';
5 class Pman_Xtuple_Import_HSBC extends Pman_Roo
9 if (HTML_FlexyFramework::get()->cli) {
12 return parent::getAuth();
17 $this->transObj = DB_DataObject::Factory('cohead');
19 $this->transObj->query('BEGIN');
21 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
23 $img = DB_DataObject::Factory('images');
26 'ontable' => 'ipshead'
28 $img->onUpload(false);
30 require_once 'File/Convert.php';
31 $fc = new File_Convert($img->getStoreName(), $img->mimetype );
32 $csv = $fc->convert('text/csv');
33 $this->importCsv($csv);
37 function importCsv($csv)
39 if(empty($_REQUEST['bankaccnt_id'])){
40 $this->jerr("invalid bank account");
43 $bankaccnt_id = (int)$_REQUEST['bankaccnt_id'];
45 $bank = DB_DataObject::factory('bankaccnt');
46 if(!$bank->get($bankaccnt_id)){
47 $this->jerr("invalid bank account");
51 ini_set("auto_detect_line_endings", true);
53 $fh = fopen($csv, 'r');
55 $this->jerr("invalid file");
59 'DATE', 'DESCRIPTION',
60 'WITHDRAWAL (HKD)', 'DEPOSIT (HKD)',
67 while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
69 if (!strlen(implode('', $n))) {
76 $cols[] = strtoupper(trim($k));
84 if (!in_array($r,$cols)) {
91 foreach($cols as $i=>$k) {
98 $this->jerr("could not find a row with " . implode(' / ', $req));
106 foreach ($rows as $row){
107 if(empty($row['DESCRIPTION'])){
110 if(!preg_match('/HK[0-9][0-9][0-9][0-9][0-9]/', $row['DESCRIPTION'], $matches)){
113 $number = $matches[0];
114 $amount = (empty($row['DEPOSIT (HKD)']) || !is_numeric($row['DEPOSIT (HKD)'])) ? 0 : $row['DEPOSIT (HKD)'];
116 $cohead = DB_DataObject::factory('cohead');
117 if(!$cohead->get('cohead_number', $number)){
118 $errMsg[] = "Could not process payments: $number - no matching order";
122 $cobmisc = DB_DataObject::factory('cobmisc');
123 $cobmisc->cobmisc_cohead_id = $cohead->pid();
124 $cobmisc->cobmisc_posted = 1;
126 if($cobmisc->count() != 1){
127 $errMsg[] = "Could not process payments: $number - no matching invoice";
131 $cobmisc->find(true);
133 $invoice = $cobmisc->invchead();
134 $aropen = $invoice->aropen();
136 $unpaid = ($aropen->aropen_amount - $aropen->aropen_paid) * 1;
139 $cr = DB_DataObject::Factory('cashrcpt');
141 'cashrcpt_amount' => $unpaid,
142 'cashrcpt_curr_id' => $cohead->cohead_curr_id,
143 'cashrcpt_fundstype' => 'C',
144 'cashrcpt_docdate' => $cr->sqlValue("NOW()::DATE"),
145 'cashrcpt_distdate' => $cr->sqlValue("NOW()::DATE"),
146 'cashrcpt_applydate' => $cr->sqlValue("NOW()::DATE"),
147 'cashrcpt_cust_id' => $cohead->cohead_cust_id,
148 'cashrcpt_salescat_id' => -1,
149 'cashrcpt_discount' => 0,
150 'cashrcpt_usecustdeposit' => 1,
151 'cashrcpt_number' => $cr->sqlValue("fetchCashRcptNumber()"),
152 'cashrcpt_bankaccnt_id' => $bank->pid(),
156 $cr->onInsert(array('cashrcpt_aropen_id' => $aropen->pid()), $this);
158 $cohead->sendInvoice($this);
160 $aropen = $invoice->aropen();
163 if(ROUND($aropen->aropen_paid,2) != ROUND($amount,2)){
164 $errMsg[] = "$number payment amount not correct! Order : $aropen->aropen_paid, HSBC : $amount";
168 $okMsg[] = "succesfully imported $number";
173 $this->jerr(implode("\n", $errMsg));
177 $this->jok(implode("<br/>", $okMsg));