Fix #7123 - getting abra ready to test
[Pman.Xtuple] / Import / CreditMemo.php
1 <?php
2
3
4 require_once 'Pman/Roo.php';
5
6 class Pman_Xtuple_Import_CreditMemo extends Pman_Roo
7 {
8     
9     /**
10      *  get .. same as roo... 
11      * 
12      */
13     
14     function getAuth()
15     {
16         if (HTML_FlexyFramework::get()->cli) {
17             return true;
18         }
19         return parent::getAuth();
20     }
21     
22     
23     function get()
24     {
25         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
26         $this->jerr("INVALID");
27         //DB_DataObject::DebugLevel(1);
28         require_once 'File/Convert.php';
29         $fc = new File_Convert('/tmp/sku.csv', 'text/csv');
30         //var_Dump($img->getStoreName());
31         $csv = $fc->convert('text/csv');
32         $this->importCsv($csv);
33     }
34     
35     
36     function post( )
37     {
38         
39         $this->sessionState(0); // turn off the session..
40         
41         $this->transObj = DB_DataObject::Factory('custinfo');
42         
43         $this->transObj->query('BEGIN');
44         
45         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
46
47         if (empty($_REQUEST['onid'])) {
48             $this->jerr('no order found');
49             
50         }
51         
52          
53         $img = DB_DataObject::Factory('images');
54         $img->setFrom(array(
55             'onid' => 0,
56             'ontable' => 'ipshead'
57         ));
58         $img->onUpload(false);
59         
60         require_once 'File/Convert.php';
61         $fc = new File_Convert($img->getStoreName(), $img->mimetype );
62         $csv = $fc->convert('text/csv');
63         $this->importCsv($csv);
64     }
65     function importCsv($csv)
66     {
67         
68         ini_set("auto_detect_line_endings", true);
69
70         $req = array('ITEM CODE', 'QTY', 'PRICE');
71         
72         $fh = fopen($csv, 'r');
73         if (!$fh) {
74             $this->jerr("invalid file");
75         }
76         
77         // we need to break this into cols..
78         $cols = false;
79         $rows = array();
80         while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
81              
82             if (!$cols) {
83                 
84                 $cols = array();
85                 foreach($n as $k) {
86                     $cols[] = strtoupper(trim($k));
87                 }
88                 if (empty($cols)) {
89                     continue;
90                 }
91                 
92                 foreach($req as $r) {
93                     if (!in_array($r,$cols)) {
94                         $cols = false;
95                         break;
96                         
97                     }
98                 }
99                 continue;
100             }
101             $row = array();
102             foreach($cols as $i=>$k) {
103                 $row[$k] = $n[$i];
104             }
105             $rows[] = $row;
106         }
107          
108         if (empty($cols)) {
109             $this->jerr("could not find a row with Item Code, Qty and Price");
110         }
111         fclose($fh);
112         
113         
114         $cmhead = DB_DataObject::Factory('cmhead');
115         if (!$cmhead->get($_REQUEST['onid'])) {
116             $this->jerr("order is not valid");
117         }
118         
119         //DB_DataObject::debugLevel(1);
120         $missing = array();
121         
122         foreach($rows as $row)
123         {
124             if (!strlen(trim(implode('', array_values($row))))) {
125                 continue;
126             }
127             if (!strlen(trim($row['ITEM CODE']))) {
128                 $missing[]  = implode('', array_values($row));
129             }
130             
131             
132             $item = DB_DataObject::factory('item')->lookupSKU($row['ITEM CODE']);
133             
134             if (!$item) {
135                 $missing[] = $row['ITEM CODE'];
136                 continue;
137             }
138             if (!empty($missing)) {
139                 continue;
140             }
141             
142             $citem = DB_DataObject::Factory('cmitem');
143             $citem->setFrom($citem->defaults());
144             $citem->setFrom(array(
145                 'cmitem_cmhead_id' => $cmhead->pid(),
146                 'cmitem_itemsite_id' => $item->itemsite()->pid(),
147                 'cmitem_qtycredit' => $row['QTY'],
148                 'cmitem_qtyreturned' => $row['QTY'],
149                 'cmitem_unitprice' => $row['PRICE'],
150                 'cmitem_comments' => $item->item_descrip1,
151                 'cmitem_linenumber' => $this->nextline($cmhead)
152                 
153             ));
154             $citem->insert();
155             
156         }
157         if (!empty($missing)) {
158             $this->jerr("missing these codes : " . implode("\n", $missing));
159         }
160         
161         $this->jok("IMPORTED");
162          
163     
164     }
165     
166     
167     function nextline($cmhead)
168     {
169         static $line = false;
170         if ($line !== false) {
171             $line++;
172             return $line;
173         }
174         $t = DB_DataObject::Factory('cmitem');
175         $t->selectAdd();
176         $t->cmitem_cmhead_id = $cmhead->pid();
177         $t->selectAdd('MAX(cmitem_linenumber) as cmitem_linenumber');
178         if (!$t->find(true)) {
179             $line = 1;
180             return $line;
181         }
182         $line = $t->cmitem_linenumber +1;
183         return $line;
184     }
185         
186         
187         
188          
189 }
190