Fix #7123 - getting abra ready to test
[Pman.Xtuple] / Import / AUPostAccounts.php
1 <?php
2
3 require_once 'Pman/Roo.php';
4
5 class Pman_Xtuple_Import_AUPostAccounts extends Pman_Roo
6 {
7     function getAuth()
8     {
9         if (HTML_FlexyFramework::get()->cli) {
10             return true;
11         }
12         return parent::getAuth();
13     }
14     
15     function post()
16     {
17         $this->transObj = DB_DataObject::Factory('custinfo');
18         
19         $this->transObj->query('BEGIN');
20         
21         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
22         
23         $img = DB_DataObject::Factory('images');
24         $img->setFrom(array(
25             'onid' => 0,
26             'ontable' => 'ipshead'
27         ));
28         $img->onUpload(false);
29         
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);
34     }
35     
36     function importCsv($csv)
37     {
38         ini_set("auto_detect_line_endings", true);
39         
40         $fh = fopen($csv, 'r');
41         if (!$fh) {
42             $this->jerr("invalid file");
43         }
44         
45         $req = array('CUSTOMER', 'ACCOUNT NO.');
46         
47         $cols = false;
48         $rows = array();
49         
50         while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
51             if (!$cols) {
52                 
53                 $cols = array();
54                 foreach($n as $k) {
55                     $cols[] = strtoupper(trim($k));
56                 }
57                 
58                 if (empty($cols)) {
59                     continue;
60                 }
61                 foreach($req as $r) {
62                     if (!in_array($r,$cols)) {
63                         $cols = false;
64                         break;
65                     }
66                 }
67                 continue;
68             }
69             foreach($cols as $i=>$k) {
70                 $row[$k] = $n[$i];
71             }
72             $rows[] = $row;
73         }
74         
75         if (empty($cols)) {
76             $this->jerr("could not find a row with " . implode(' / ', $req));
77         }
78         
79         fclose($fh);
80         
81         $missing = array();
82         foreach ($rows as $row){
83             if(empty($row['CUSTOMER']) || empty($row['ACCOUNT NO.'])){
84                 continue;
85             }
86             $custinfo = DB_DataObject::factory('custinfo');
87             if(!$custinfo->get('cust_name',$row['CUSTOMER'])){
88                 $missing[] = $row['CUSTOMER'];
89                 continue;
90             }
91             
92             $ca = DB_DAtaObject::factory('charass');
93             $ca->query("SELECT
94                     charass_setvalue(
95                        'C',{$custinfo->pid()},
96                        'AU-POST-ACCNO', '{$row['ACCOUNT NO.']}'
97                     );");
98             
99         }
100         
101         $this->jok('data imported successfully! <br/> Missing : <br/>' . implode("<br/>", $missing));
102         
103         
104         exit;
105     }
106 }