PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/its/returns/dispatch_returns_controller.php

https://code.google.com/
PHP | 221 lines | 179 code | 28 blank | 14 comment | 23 complexity | 94d72eff87e5faff29ffa4aa7786e6ad MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. class DispatchReturnsController extends AppController {
  3. var $name = 'DispatchReturns';
  4. var $viewPath = 'its/returns/dispatch_returns';
  5. var $pageTitle = 'Dispatch Return Information';
  6. var $uses = array('DRH', 'DR', 'Dispatch', 'DispatchStatus', 'CustStockIn', 'Product', 'ProductSerial', 'RunningNo', 'Location', 'UserCoH');
  7. var $paginate = array(
  8. 'DRH' => array(
  9. 'order' => 'DRH.id DESC'
  10. )
  11. );
  12. function beforeFilter() {
  13. parent::beforeFilter();
  14. $this->set('parentNav', 'its_returns');
  15. $this->set('tabMenu', 'tabmenus/its/returns');
  16. }
  17. function display() {
  18. parent::display('DRH');
  19. $this->paginate['DRH']['contain'] = array('Customer', 'Site', 'Dispatch', 'Warehouse');
  20. $this->_checkCustomerOnHand('DRH');
  21. $op = array(
  22. 'customer_id' => '=',
  23. );
  24. $this->_paging('DRH', $op);
  25. }
  26. function save() {
  27. $is_success = true;
  28. $drh_id = null;
  29. if ($this->data['mode'] == 'add') {
  30. $is_success = $this->_save_return();
  31. } else { // mode == 'edit'
  32. $this->DRH->id = $this->data['DRH']['id'];
  33. $is_success = $this->DRH->saveField('remark', $this->data['DRH']['remark']);
  34. $is_success &= $this->DRH->saveField('collector_id', $this->Auth->user('id'));
  35. }
  36. if ($is_success) {
  37. $drh_id = $this->DRH->id;
  38. $this->Session->setFlash("Save succesfull.", 'flash/ok');
  39. } else {
  40. $this->Session->setFlash("Save fail !", 'flash/fail');
  41. }
  42. $this->data['DR'] = null;
  43. $this->setAction('view', $drh_id);
  44. }
  45. function _save_return() {
  46. $drh_id = null;
  47. $is_success = true;
  48. // ตรวจสอบว่ามีรายการเข้ามาหรือไม่
  49. if (!empty($this->data['DRH']['dispatch_id'])) {
  50. $this->DRH->query("BEGIN");
  51. ;
  52. // กำหนดข้อมูล Dispatch Return Head
  53. // Generate return no
  54. $year = $this->Utility->getThaiYear();
  55. $runno = $this->RunningNo->getNextStockNo(RunningNo::DISPATCH_RETURN_TYPE, $year);
  56. $this->data['DRH']['return_no'] = RunningNo::DISPATCH_RETURN_TYPE . $year . $runno;
  57. $this->data['DRH']['collector_id'] = $this->Auth->user('id');
  58. $this->ProductSerial->validate['location_id'] = array('rule' => 'notEmpty', 'message' => 'Please select a product serial location.');
  59. if ($this->DRH->saveAll($this->data, array('validate' => 'first', 'atomic' => false)) == true) { // ถ้าไม่ใช่จะ return array error field
  60. // update Dispatch ว่ามีการ Return
  61. $this->Dispatch->id = $this->data['DRH']['dispatch_id'];
  62. $this->Dispatch->saveField('is_dispatch_return', true);
  63. // ถ้าเป็น On Dispatch เปลี่ยนเป็น Cancel
  64. if ($this->data['dispatch_status_id'] == DispatchStatus::ON_DISPATCH_STATUS) {
  65. $this->Dispatch->saveField('dispatch_status_id', DispatchStatus::CANCEL_STATUS);
  66. }
  67. $drh_id = $this->DRH->id;
  68. foreach ($this->data['DR'] as $dR) {
  69. // Update Cust Product Site current stock
  70. $return_qty = $dR['return_qty'];
  71. $this->Product->id = $dR['product_id'];
  72. $current_stock = $this->Product->field('current_stock');
  73. $is_success &= $this->Product->saveField('current_stock', $current_stock + $return_qty);
  74. // Update CustStockIn Dispatch Status
  75. $this->CustStockIn->id = $dR['cust_stock_in_id'];
  76. $dispatch_status_id = $this->CustStockIn->field('dispatch_status_id');
  77. if ($dispatch_status_id == DispatchStatus::ON_DISPATCH_STATUS) {
  78. $this->CustStockIn->saveField('dispatch_status_id', DispatchStatus::CANCEL_STATUS);
  79. }
  80. // ดึงรายการ dispatch_return
  81. $this->DR->contain();
  82. $_dr = $this->DR->find('first', array(
  83. 'conditions' => array(
  84. 'DR.product_id' => $dR['product_id'],
  85. 'dispatch_return_head_id' => $drh_id
  86. )
  87. )
  88. );
  89. // คัดลอก ProductSerial เดิม มาใส่อันใหม่
  90. for ($i = 0; $i < $return_qty; ++$i) {
  91. $this->ProductSerial->contain();
  92. $ps = $this->ProductSerial->findById($dR['ProductSerial'][$i]['id']);
  93. $data = array();
  94. $data['ProductSerial']['id'] = null;
  95. $data['ProductSerial']['product_id'] = $_dr['DR']['product_id'];
  96. $data['ProductSerial']['serial_no'] = $ps['ProductSerial']['serial_no'];
  97. $data['ProductSerial']['other_information'] = $ps['ProductSerial']['other_information'];
  98. $data['ProductSerial']['cost'] = $ps['ProductSerial']['cost'];
  99. $data['ProductSerial']['price'] = $ps['ProductSerial']['price'];
  100. $data['ProductSerial']['location_id'] = $dR['ProductSerial'][$i]['location_id'];
  101. $data['ProductSerial']['its_stock_in_type'] = DR::ITS_STOCK_IN_TYPE;
  102. $data['ProductSerial']['its_stock_in_id'] = $_dr['DR']['id'];
  103. $data['ProductSerial']['stock_out_priority'] = $ps['ProductSerial']['stock_out_priority'];
  104. $data['ProductSerial']['is_active_flag'] = true;
  105. // update ProductSerial เดิมว่าใช้งานไม่ได้
  106. $this->ProductSerial->id = $ps['ProductSerial']['id'];
  107. $this->ProductSerial->saveField('is_active_flag', false);
  108. // บันทึก ProductSerial อันใหม่
  109. $this->ProductSerial->create();
  110. $is_success &= $this->ProductSerial->save($data);
  111. // debug($this->ProductSerial->findById($this->ProductSerial->id));
  112. }
  113. }
  114. } else {
  115. $is_success = false;
  116. }
  117. if ($is_success) {
  118. $this->DRH->query("COMMIT");
  119. ;
  120. } else {
  121. $this->passedArgs['dispatch_id'] = $this->data['DRH']['dispatch_id'];
  122. $this->DRH->query("ROLLBACK");
  123. }
  124. } else {
  125. $this->data = null;
  126. return false;
  127. }
  128. return $is_success;
  129. }
  130. function view($id = null) {
  131. // edit หรือมาจากหน้า dispatch
  132. if ($id != null) {
  133. $this->DRH->contain(array('Customer', 'Site', 'Dispatch', 'Warehouse', 'DR' => array('Product' => array('Unit'))));
  134. if (isset($this->passedArgs['return_dispatch_id'])) {
  135. $this->data = $this->DRH->findByDispatchId($this->passedArgs['return_dispatch_id']);
  136. } else {
  137. $this->data = $this->DRH->findById($id);
  138. }
  139. $this->_neighbors($id, $this->DRH, array('fields' => array('id')));
  140. $this->set('mode', 'edit');
  141. } else {
  142. // ดึงรายการ Dispatch ที่ Partial หรือ On Dispatch ที่ยังไม่ได้ทำการ Return
  143. $this->set('dispatches', $this->Dispatch->find('list', array(
  144. 'conditions' => array(
  145. 'dispatch_status_id' => array(
  146. DispatchStatus::ON_DISPATCH_STATUS,
  147. DispatchStatus::PARTIAL_CONSIGNED_STATUS
  148. ),
  149. 'is_dispatch_return' => null
  150. )
  151. )
  152. )
  153. );
  154. if (isset($this->passedArgs['dispatch_id'])) {
  155. $this->CustStockIn->bindModel(array('hasMany' => array(
  156. 'ProductSerial' => array(
  157. 'foreignKey' => 'its_stock_out_id',
  158. 'conditions' => array(
  159. 'its_stock_out_type' => CustStockIn::ITS_STOCK_OUT_TYPE,
  160. 'cust_stock_in_id' => null, // เฉพาะ ที่ไม่ได้ stock in
  161. 'is_active_flag' => true // เฉพาะ ที่ยังไม่ได้ return
  162. )
  163. )
  164. )
  165. )
  166. );
  167. $this->Dispatch->contain(array('Customer', 'Site', 'Warehouse',
  168. 'CustStockIn' => array(
  169. 'Product' => array('Unit'), 'ProductSerial',
  170. 'conditions' => array(
  171. 'CustStockIn.dispatch_status_id' => array(
  172. DispatchStatus::ON_DISPATCH_STATUS,
  173. DispatchStatus::CANCEL_STATUS,
  174. DispatchStatus::PARTIAL_CONSIGNED_STATUS
  175. )
  176. )
  177. )
  178. )
  179. );
  180. $this->data = $this->Dispatch->findById($this->passedArgs['dispatch_id']);
  181. $this->set('locations', $this->Location->find('list', array('conditions' => array('warehouse_id' => $this->data['Dispatch']['warehouse_id']))));
  182. }
  183. $this->set('mode', 'add');
  184. $this->set('return_by', $this->Auth->user('name'));
  185. $this->set('today', date("d/m/Y"));
  186. }
  187. }
  188. function print_view($id) {
  189. $this->layout = 'print';
  190. $this->data = $this->DRH->findForPrintById($id);
  191. }
  192. }
  193. ?>