Fix #7123 - getting abra ready to test
[Pman.Xtuple] / Import / PurchaseOrder.php
1 <?php
2
3 require_once 'Pman/Roo.php';
4
5 class Pman_Xtuple_Import_PurchaseOrder 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         $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         $pohead_id = $_REQUEST['pohead_id'];
46         
47         $req = array('ITEM CODE', 'ITEM DESCRIPTION', 'DUE DATE', 'LINE NUMBER', 'ORDERED', 'UNIT PRICE');
48         
49         $cols = false;
50         $rows = array();
51         
52         while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
53             if (!$cols) {
54                 
55                 $cols = array();
56                 foreach($n as $k) {
57                     $cols[] = strtoupper(trim($k));
58                 }
59                 if (empty($cols)) {
60                     continue;
61                 }
62                 foreach($req as $r) {
63                     if (!in_array($r,$cols)) {
64                         $cols = false;
65                         break;
66                     }
67                 }
68                 continue;
69             }
70             foreach($cols as $i=>$k) {
71                 $row[$k] = $n[$i];
72             }
73             $rows[] = $row;
74         }
75         
76         if (empty($cols)) {
77             $this->jerr("could not find a row with " . implode(' / ', $req));
78         }
79         
80         fclose($fh);
81         
82         // check pohead
83         $pohead = DB_DataObject::factory('pohead');
84         if(!$pohead->get($pohead_id)){
85             $this->jerr('error occur on finding pohead');
86         }
87         
88         // check poitem linenumber
89         $max_num = 0;
90         $poitem = DB_DataObject::factory('poitem');
91         $poitem->selectAdd();
92         $poitem->selectAdd("
93             MAX(poitem_linenumber) as max_num
94         ");
95         $poitem->poitem_pohead_id = $pohead->pid();
96         $poitem->find(true);
97         if(!empty($poitem->max_num)){
98             $max_num = $poitem->max_num;
99         }
100         
101         foreach($rows as $i => $row){
102             /* 
103              * check item and itemsite and itemsrc, 
104              * throw exception while the item or itemsite or itemsrc is not exist for the time being
105              * 
106              * we might need to new the item in the further
107              */
108             $item = DB_DataObject::factory('item');
109             if(!$item->get('item_number', $row['ITEM CODE'])){
110                 $this->jerr('item ' . $row['ITEM CODE'] . ' does not exist');
111             }
112             $itemsite = DB_DataObject::factory('itemsite');
113             if(!$itemsite->get('itemsite_item_id', $item->pid())){
114                 $this->jerr($row['ITEM CODE'] . ' does not exist in item site');
115             }
116             $itemsrc = DB_DataObject::factory('itemsrc');
117             if(!$itemsrc->get('itemsrc_item_id', $item->pid())){
118                 $this->jerr($row['ITEM CODE'] . ' does not exist in item src');
119             }
120             
121             // create poitem 
122             $poitem = DB_DataObject::factory('poitem');
123             $poitem->setFrom(array(
124                 'poitem_status' => $pohead->pohead_status,
125                 'poitem_pohead_id' => $pohead->pid(),
126                 'poitem_linenumber' => $max_num + ($i + 1),
127                 'poitem_duedate' => date('Y-m-d',strtotime($row['DUE DATE'])),
128                 'poitem_itemsite_id' => $itemsite->pid(),
129                 'poitem_qty_ordered' => $row['ORDERED'],
130                 'poitem_unitprice' => $row['UNIT PRICE'],
131                 'poitem_itemsrc_id' => $itemsrc->pid()
132             ));
133             
134             foreach($poitem->defaults() as $k => $v){
135                 if (!isset($poitem->$k)) {
136                     $poitem->$k = $v;
137                 }
138             }
139             
140             $poitem->insert();
141             
142             if(!$poitem->pid()){
143                 $this->jerr('error occur on insert poitem ' . $row['ITEM CODE']);
144             }
145             
146         }
147         
148         $this->jok('DONE');
149         
150         exit;
151     }
152 }