Fix #7123 - getting abra ready to test
[Pman.Xtuple] / Import / AUPurchaseOrder.php
1 <?php
2
3 require_once 'Pman/Roo.php';
4
5 class Pman_Xtuple_Import_AUPurchaseOrder 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('poitem');
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         
32         $fc = new File_Convert($img->getStoreName(), $img->mimetype );
33         $csv = $fc->convert('text/csv');
34         $this->importCsv($csv);
35     }
36     
37     function importCsv($csv)
38     {
39         ini_set("auto_detect_line_endings", true);
40         
41         $fh = fopen($csv, 'r');
42         if (!$fh) {
43             $this->jerr("invalid file");
44         }
45         
46         $pohead_id = $_REQUEST['pohead_id'];
47         
48         $req = array('ITEM', 'ITEM DESCRIPTION', 'ON HAND', 'AVG COST');
49         
50         $cols = false;
51         $rows = array();
52         
53         while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
54             if(!array_filter($n)){ // empty row
55                 continue;
56             }
57             if (!$cols) {
58                 
59                 $cols = array();
60                 foreach($n as $k) {
61                     $cols[] = strtoupper(trim($k));
62                 }
63                 if (empty($cols)) {
64                     continue;
65                 }
66                 foreach($req as $r) {
67                     if (!in_array($r,$cols)) {
68                         $cols = false;
69                         break;
70                     }
71                 }
72                 continue;
73             }
74             
75             foreach($cols as $i=>$k) {
76                 $row[$k] = $n[$i];
77             }
78             $rows[] = $row;
79         }
80         
81         if (empty($cols)) {
82             $this->jerr("could not find a row with " . implode(' / ', $req));
83         }
84         
85         fclose($fh);
86         
87         //print_R($rows);exit;
88         
89         // check pohead
90         $pohead = DB_DataObject::factory('pohead');
91         if(!$pohead->get($pohead_id)){
92             $this->jerr('error occur on finding pohead');
93         }
94         
95         $max_num = 0;
96         $poitem = DB_DataObject::factory('poitem');
97         $poitem->selectAdd();
98         $poitem->selectAdd("
99             MAX(poitem_linenumber) as max_num
100         ");
101         $poitem->poitem_pohead_id = $pohead->pid();
102         $poitem->find(true);
103         if(!empty($poitem->max_num)){
104             $max_num = $poitem->max_num;
105         }
106         
107         $missing = array();
108         foreach ($rows as $i => $row){
109             if (empty($row['ON HAND']) || $row['ON HAND'] < 1) {
110                 continue;
111             }
112             if(empty($row['ITEM'])){
113                 unset($rows[$i]);
114                 continue;
115             }
116             
117             $item = DB_DataObject::factory('item')->lookupSKU($row['ITEM']);
118             
119             if(!$item){
120                 
121                 $missing[] = $row['ITEM']  . ' does not exist';
122                 continue;
123             }
124             
125             $itemsite = DB_DataObject::factory('itemsite');
126             if(!$itemsite->get('itemsite_item_id', $item->pid())){
127                 $missing[] = $row['ITEM'] . ' does not exist in item site';
128                 continue;
129             }
130             $rows[$i]['ITEMSITE ID'] = $itemsite->pid();
131             
132         }
133         
134         if(count($missing)){
135             $this->jerr(implode("\n", $missing));
136         }
137         
138         foreach($rows as $i => $row){
139             // create poitem
140              if (empty($row['ON HAND']) || $row['ON HAND'] < 1) {
141                 continue;
142             }
143             
144             $poitem = DB_DataObject::factory('poitem');
145             $poitem->setFrom(array(
146                 'poitem_status' => $pohead->pohead_status,
147                 'poitem_pohead_id' => $pohead->pid(),
148                 'poitem_linenumber' => $max_num + ($i + 1),
149                 'poitem_duedate' => date('Y-m-d'),
150                 'poitem_itemsite_id' => $row['ITEMSITE ID'],
151                 'poitem_qty_ordered' => $row['ON HAND'],
152                 'poitem_unitprice' => $row['AVG COST'],
153                 'poitem_itemsrc_id' => $poitem->sqlValue('NULL') 
154             ));
155             
156             foreach($poitem->defaults() as $k => $v){
157                 if (!isset($poitem->$k)) {
158                     $poitem->$k = $v;
159                 }
160             }
161             
162             $poitem->insert();
163             
164             if(!$poitem->pid()){
165                 $this->jerr('error occur on insert poitem ' . $row['ITEM CODE']);
166             }
167             
168         }
169         $this->jok('IMPORTED');
170         
171         exit;
172     }
173 }