PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/suppliers/delivery/supplier_inventories_controller.php

https://code.google.com/
PHP | 178 lines | 130 code | 28 blank | 20 comment | 14 complexity | 5650c4da3de07f333a127d0edba92944 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. class SupplierInventoriesController extends AppController {
  3. var $name = 'SupplierInventories';
  4. var $pageTitle = 'Customer Inventory';
  5. var $viewPath = 'suppliers/delivery/supplier_inventories';
  6. var $uses = array('CustProductSite', 'Site', 'Customer', 'Supplier', 'Category');
  7. /**
  8. * สำหรับ Site user ที่มีสิทธิ์ can_view_other_site เลือกดูข้อมูล site อื่น
  9. * @var int site id ที่ต้องการดู
  10. */
  11. var $viewSiteInventoryId = null;
  12. function beforeFilter() {
  13. parent::beforeFilter();
  14. $this->set('parentNav', 'supplier_inventories');
  15. }
  16. function _getSupplierId() {
  17. // Customer เป็นคนดู ส่ง Supplier_id ที่ต้องการดูมาใน named params
  18. if (!empty($this->passedArgs['supplier_id'])) {
  19. $supplier_id = $this->passedArgs['supplier_id'];
  20. } else {
  21. $supplier_id = $this->Auth->user('supplier_id');
  22. }
  23. return $supplier_id;
  24. }
  25. /**
  26. * ใช้ทั้งฝั่ง Supplier และ Customer ในการดูคลังลูกค้าที่ Supplier
  27. * @param type $customer_id
  28. * @param type $site_id
  29. */
  30. function display($customer_id, $site_id = null) {
  31. parent::display('CustProductSite');
  32. $supplier_id = $this->_getSupplierId();
  33. if ($site_id == null) {
  34. $site = $this->Site->findByCustomerId($customer_id);
  35. $site_id = $site['Site']['id'];
  36. }
  37. $op = array(
  38. 'supplier_part_no' => 'LIKE',
  39. 'category_id' => '=',
  40. 'product_id' => '=',
  41. 'brand_id' => '=',
  42. 'model' => 'LIKE',
  43. 'title' => 'LIKE',
  44. 'text' => 'LIKE',
  45. 'supplier_part_no' => 'LIKE',
  46. 'stock_code' => 'LIKE',
  47. 'supplier_current_stock' => !empty($this->data['Operand']['supplier_current_stock']) ? $this->data['Operand']['supplier_current_stock'] : '>',
  48. 'reserve_qty' => !empty($this->data['Operand']['reserve_qty']) ? $this->data['Operand']['reserve_qty'] : '>',
  49. 'min_stock' => !empty($this->data['Operand']['min_stock']) ? $this->data['Operand']['min_stock'] : '>',
  50. 'max_stock' => !empty($this->data['Operand']['max_stock']) ? $this->data['Operand']['max_stock'] : '>',
  51. );
  52. $this->paginate['CustProductSite']['conditions']['CustProductSite.customer_id'] = $customer_id;
  53. $this->paginate['CustProductSite']['conditions']['CustProductSite.supplier_id'] = $supplier_id;
  54. $this->paginate['CustProductSite']['contain'] = array('Product' => array('Category', 'Brand'));
  55. if ($this->Auth->user('prefix') == User::USER_PREFIX_CUSTOMER) {
  56. $this->pageTitle = 'Vendor Inventory';
  57. }
  58. $this->set('supplier_id', $supplier_id);
  59. $this->Supplier->contain();
  60. $this->set('supplier', $this->Supplier->findById($supplier_id));
  61. $this->Customer->contain();
  62. $this->set('customer', $this->Customer->findById($customer_id));
  63. $this->set('customer_id', $customer_id);
  64. $this->Site->contain();
  65. $this->set('site', $this->Site->findById($site_id));
  66. $this->set('site_id', $site_id);
  67. // debug($this->data);
  68. $this->_paging('CustProductSite', $op);
  69. }
  70. function view($id) {
  71. if ($id != null) {
  72. $this->CustProductSite->contain(array('Product' => array('Category', 'Brand', 'Unit'), 'ShortestPeriod', 'MaintenancePeriod', 'CyclePeriod', 'Supplier'));
  73. $this->data = $this->CustProductSite->findById($id);
  74. $this->_neighbors($id, $this->CustProductSite, array(
  75. 'field' => 'stock_code', 'value' => $this->data['CustProductSite']['stock_code'],
  76. 'fields' => array('id', 'stock_code'),
  77. 'contain' => array('Site',
  78. 'Product.Supplier', 'Product.Category', 'Product.Brand', 'Product.supplier_part_no', 'Product.part_no', 'Product.model', 'Product.title', 'Product.text',
  79. 'ProductSerial' => array('CustLocation', 'limit' => 1, 'order' => 'stock_out_priority')
  80. ))
  81. );
  82. $this->set('customer_id', $this->data['CustProductSite']['customer_id']);
  83. $this->set('site_id', $this->data['CustProductSite']['site_id']);
  84. $this->set('supplier_id', $this->data['CustProductSite']['supplier_id']);
  85. }
  86. }
  87. function popup_search() {
  88. parent::popup_search();
  89. $supplier_id = $this->_getSupplierId();
  90. $customer_id = $this->passedArgs['customer_id'];
  91. $site_id = $this->passedArgs['site_id'];
  92. $this->set('operands', array('<' => '<', '<=' => '<=', '=' => '=', '>' => '>', '>=' => '>='));
  93. $this->set('categories', $this->Category->listCategoryBySupplierId($supplier_id));
  94. $this->set('popupWidth', 800);
  95. $this->set('params', $customer_id . '/' . $site_id . '/supplier_id:' . $supplier_id);
  96. }
  97. /**
  98. * popup นี้จะเหมือนหน้า display เอามาแสดงเป็น popup สามารถเลือก product แล้วส่ง part no กลับไปหน้าที่เรียกมาได้
  99. */
  100. function popup_view() {
  101. $this->layout = 'popup';
  102. // clear session การ search จากครั้งก่อน
  103. $this->Session->delete('Product');
  104. $this->Session->delete('Products');
  105. $this->Session->delete("CustProductSite.paging.site_id");
  106. $this->Session->delete("CustProductSite.paging.customer_id");
  107. $supplier_id = $this->Auth->user('supplier_id');
  108. // ถ้ามีการส่ง site_id เข้ามา เพิ่มในเงื่อนไขการ search เลย
  109. if (isset($this->passedArgs['site_id'])) {
  110. $site_id = $this->passedArgs['site_id'];
  111. $this->Site->id = $site_id;
  112. $customer_id = $this->Site->field('customer_id');
  113. $this->set('sites', $this->Site->find('list', array('order' => 'customer_id, name', 'conditions' => array('customer_id' => $customer_id))));
  114. } else {
  115. $customer_id = null;
  116. $site_id = null;
  117. $this->set('sites', null);
  118. }
  119. $this->set('popupTitle', 'Select a Product');
  120. $this->set('popupWidth', '$j(document).width() - 100');
  121. // id ที่จะถูกเลือกเป็น default
  122. $this->set('site_id', $site_id);
  123. $this->set('customer_id', $customer_id);
  124. $this->set('categories', $this->Category->listCategoryBySupplierId($supplier_id));
  125. $this->set('brands', $this->Product->Brand->find('list', array('conditions' => array('supplier_id' => $supplier_id))));
  126. $this->set('customers', $this->VSCustomer->findListByCustomerOnHand($this->Auth->user('id'), $supplier_id));
  127. }
  128. /**
  129. * ใช้ร่วมกับ popup_view เป็นส่วนตารางแสดงแถว customers
  130. */
  131. function popup_paginate() {
  132. $this->Product->bindModel(array('hasMany' => array('CustProductSite')));
  133. if ($this->Session->check("{$this->name}.site_id")) {
  134. $this->data['CustProductSite']['site_id'] = $this->Session->read("{$this->name}.site_id");
  135. }
  136. if ($this->Session->check("{$this->name}.customer_id")) {
  137. $this->data['CustProductSite']['customer_id'] = $this->Session->read("{$this->name}.customer_id");
  138. }
  139. if (isset($this->passedArgs['site_id'])) {
  140. $this->data['CustProductSite']['site_id'] = $this->passedArgs['site_id'];
  141. $this->Session->write("{$this->name}.site_id", $this->data['CustProductSite']['site_id']);
  142. }
  143. if (isset($this->passedArgs['customer_id'])) {
  144. $this->data['CustProductSite']['customer_id'] = $this->passedArgs['customer_id'];
  145. $this->Session->write("{$this->name}.customer_id", $this->data['CustProductSite']['customer_id']);
  146. }
  147. $this->display();
  148. }
  149. }
  150. ?>