PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 1ms

/magehelp/application/controllers/procurement.php

https://bitbucket.org/jit_bec/shopifine
PHP | 2916 lines | 2106 code | 420 blank | 390 comment | 350 complexity | 307cea1c9ee6777683149faca5454931 MD5 | raw file
Possible License(s): LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. require_once ("secure_area.php");
  3. class Procurement extends Secure_area
  4. {
  5. private $user;
  6. private $username;
  7. function __construct()
  8. {
  9. parent::__construct('procurement');
  10. $this->user= $this->User->get_logged_in_employee_info();
  11. $this->username = $this->user->last_name." ".$this->user->first_name;
  12. $param = array('user' => $this->user->username);
  13. $this->load->library('acl-library/Acl',$param,'Acl');
  14. }
  15. //moved to parent class
  16. // function assignEntityToUser(){
  17. // $ids = $_REQUEST['ids'];
  18. // $entity = $_REQUEST['entity'];
  19. // $role = $_REQUEST['role'];
  20. // $user_id = $_REQUEST['user_id'];
  21. // if ($entity=='rfq'){
  22. // $model = 'Request_quote_master';
  23. // }
  24. // else if ($entity=='quote'){
  25. // $model = 'Purchase_quote_master';
  26. // }
  27. // else if ($entity=='order'){
  28. // $model = 'Purchase_order_master';
  29. // }
  30. // else if ($entity=='receipt_item'){
  31. // $model = 'Receipt_item';
  32. // }
  33. // else if ($entity=='receipt'){
  34. // $model = 'Receipt_master';
  35. // }
  36. // else if ($entity=='invoice'){
  37. // $model = 'Purchase_invoice_master';
  38. // }
  39. // if (!empty($ids) && !empty($role) ){
  40. // if (empty($user_id)){
  41. // $user_id = $this->user->person_id;
  42. // }
  43. // if ($role =='approver'){
  44. // $upd_data['approved_by'] = $user_id;
  45. // }
  46. // else if ($role =='receiver'){
  47. // $upd_data['received_by'] = $user_id;
  48. // $upd_data['owner_id'] = $user_id;
  49. // }
  50. // else if ($role =='payer'){
  51. // $upd_data['payer_id'] = $user_id;
  52. // $upd_data['owner_id'] = $user_id;
  53. // }
  54. // else if ($role =='owner'){
  55. // $upd_data['owner_id'] = $user_id;
  56. // }
  57. // foreach($ids as $id){
  58. //
  59. // $this->$model->update(array('id'=>$id),$upd_data);
  60. // }
  61. // }
  62. // }
  63. // function prepareAssignDialog(){
  64. //// var_dump(($this->User->getAllEligibleOwners('rfq')));
  65. //// var_dump(($this->User->getAllEligibleApprovers('rfq')));
  66. // $entity = $_REQUEST['entity'];
  67. // if ($entity=='order'){
  68. // $owners = $this->User->getAllEligibleReceivers($_REQUEST['entity']);
  69. // }
  70. // else{
  71. // $owners = $this->User->getAllEligibleOwners($_REQUEST['entity']);
  72. // }
  73. //
  74. //
  75. // $approvers = $this->User->getAllEligibleApprovers($_REQUEST['entity']);
  76. // $adminUserOptions =null;
  77. // $userOptions = null;
  78. // foreach($owners as $owner) {
  79. //
  80. // $name=$owner["username"];
  81. // $id=$owner["person_id"];
  82. // if (!empty($name)){
  83. // $userOptions.="<OPTION VALUE=\"$id\">".$name;
  84. // }
  85. // }
  86. // foreach($approvers as $approver) {
  87. //
  88. // $name=$approver["username"];
  89. // $id=$approver["person_id"];
  90. // if (!empty($name)){
  91. // $adminUserOptions.="<OPTION VALUE=\"$id\">".$name;
  92. // }
  93. // }
  94. // $data['userOptions'] = $userOptions;
  95. // $data['adminUserOptions'] = $adminUserOptions;
  96. // echo json_encode($data);
  97. // }
  98. /* PURCHASE QUOTE GRID START */
  99. function index()
  100. {
  101. $users = $this->User->getAllUsersByRole(null,array('field_name'=>'role_name',array('Everyone','Guest')));
  102. foreach($users as $user) {
  103. $user_id=$user["person_id"];
  104. $username=$user["username"];
  105. //$value = $denom["denom_json"];
  106. if (!empty($user_id)){
  107. $userOptions.="<OPTION VALUE=\"$user_id\">".$username;
  108. }
  109. }
  110. $data['userOptions'] = $userOptions;
  111. $this->load->view("procurement/quote/purchase_quote_grid",$data);
  112. }
  113. /* common functions */
  114. function populateSuppliers(){
  115. echo populateSuppliers();
  116. }
  117. function getCostPrice(){
  118. $id = $_REQUEST['productid'];
  119. $productDetails = $this->Product_price->getByProductId($id);
  120. echo $productDetails->cost_price;
  121. }
  122. function getPriceFromPricelist(){
  123. $product_id = $_REQUEST['product_id'];
  124. $pricelist_id = $_REQUEST['pricelist_id'];
  125. $supplier_id = $_REQUEST['supplier_id'];
  126. $results = $this->Supplier_rate_items->getPriceForProduct($product_id,$pricelist_id,$supplier_id);
  127. echo $results[0]['base_price'] ;
  128. }
  129. /* end common */
  130. /*start quote */
  131. function populateRFQ(){
  132. $quotedata=array();
  133. $status = $_REQUEST['_status'];
  134. $where = array();
  135. $in_where = array();
  136. setOwnerStatusCommon($where,$in_where);
  137. $griddata= populateGridCommon('Request_quote_master',$where,null,$in_where);
  138. $dbrows = $griddata['db_data'];
  139. $data = $griddata['grid_metadata'];
  140. foreach ($dbrows as $dp){
  141. if ($status==self::WAITING_FOR_APPROVAL){
  142. /* we dont need actions column in approval grid . So not passing the blank */
  143. array_push($quotedata, array('id'=> $dp['id'],'dprow' => array($dp['reference'],$dp['supplier_name'],$dp['estimated_value'],$dp['owner_id'],$dp['status'],$dp['raised_by_name'],$dp['owner_name'],$dp['needed_by_date'],$dp['approved_by_name'],$dp['supplier_id'],$dp['warehouse_id'],$dp['approved_by'])));
  144. }
  145. //remove
  146. // else if ($status==self::REJECTED){
  147. // /* Add Extra Columns Rejected and Rejected Notes Remove Status */
  148. // array_push($quotedata, array('id'=> $dp['id'],'dprow' => array($dp['reference'],$dp['supplier_name'],$dp['estimated_value'],$dp['raised_by_name'],$dp['owner_name'],$dp['needed_by_date'],$dp['approved_by_name'],$dp['approval_notes'])));
  149. // }
  150. // //remove
  151. // else if ($status==self::OPEN){
  152. // array_push($quotedata, array('id'=> $dp['id'],'dprow' => array('',$dp['reference'],$dp['supplier_name'],$dp['estimated_value'],$dp['status'],$dp['raised_by_name'],$dp['owner_name'],$dp['needed_by_date'])));
  153. // }
  154. else if (is_array($status)){
  155. array_push($quotedata, array('id'=> $dp['id'],'dprow' => array($dp['reference'],$dp['supplier_name'],$dp['estimated_value'],$dp['owner_id'],$dp['status'],$dp['raised_by_name'],$dp['owner_name'],$dp['needed_by_date'],$dp['approved_by_name'],$dp['supplier_id'],$dp['warehouse_id'])));
  156. }
  157. else {
  158. array_push($quotedata, array('id'=> $dp['id'],'dprow' => array($dp['reference'],$dp['supplier_name'],$dp['estimated_value'],$dp['status'],$dp['raised_by_name'],$dp['owner_name'],$dp['needed_by_date'],$dp['notes'],$dp['approved_by_name'],$dp['approval_notes'])));
  159. }
  160. }
  161. $data['quotedata'] = $quotedata;
  162. echo json_encode($data);
  163. }
  164. function createRFQ(){
  165. $id = $_REQUEST['quoteId'];
  166. $purchase_quote_data['supplier_id'] = $_REQUEST['supplierId'];
  167. $purchase_quote_data['warehouse_id'] = $_REQUEST['warehouseId'];
  168. $dateObj = DateTime::createFromFormat('d/m/Y', $_REQUEST['reqdate']);
  169. log_message('debug','converted '.$dateObj->format('Y-m-d'));
  170. $purchase_quote_data['needed_by_date'] = $dateObj->format('Y-m-d');
  171. $comments = appendComments($_REQUEST['notes'], 'notes');
  172. $purchase_quote_data['owner_id'] = $this->user->person_id;
  173. if (empty($id)){
  174. $purchase_quote_data['raised_by'] = $this->user->person_id;
  175. $id = $this->Request_quote_master->createRequest($purchase_quote_data,array('notes'=>$comments));
  176. }
  177. else {
  178. $where_clause = array('id'=>$id);
  179. $this->Request_quote_master->update($where_clause, $purchase_quote_data,array('notes'=>$comments));
  180. }
  181. echo $id;
  182. }
  183. //this function validates the record after close button of dialog being pressed. If no items are aadded the record is deleted
  184. function closeValidate(){
  185. $id = $_REQUEST['id'];
  186. $entity = $_REQUEST['entity'];
  187. $items_count = $_REQUEST['items_count'];
  188. if ($entity=='quote'){
  189. $model = 'Purchase_quote_master';
  190. $itemModel= 'Purchase_quote_item';
  191. $where_clause_quote_item = array('quote_id'=>$id);
  192. }
  193. else if ($entity=='rfq'){
  194. $model = 'Request_quote_master';
  195. $itemModel= 'Request_quote_item';
  196. $where_clause_quote_item = array('rfq_id'=>$id);
  197. }
  198. if (!empty($id)){
  199. if ($items_count>0){
  200. $where_clause_quote = array('id'=>$id);
  201. $this->$model->update($where_clause_quote,array('status'=>self::OPEN));
  202. $this->$itemModel->update($where_clause_quote_item,array('status'=>self::OPEN));
  203. }
  204. }
  205. }
  206. function modifyRFQ (){
  207. $id = $_REQUEST['id'];
  208. $oper = $_REQUEST['oper'];
  209. $this->db->trans_start();
  210. if ($oper=='edit'){
  211. if (!empty($_REQUEST['needed_date'])){
  212. $dateObj = DateTime::createFromFormat('d/m/Y', $_REQUEST['needed_date']);
  213. log_message('debug','converted item date '.$dateObj->format('Y-m-d'));
  214. $purchase_quote_data['needed_by_date'] = $dateObj->format('Y-m-d');
  215. }
  216. if (!empty($_REQUEST['warehouse_id'])){
  217. $purchase_quote_data['warehouse_id'] = $_REQUEST['warehouse_id'];
  218. }
  219. $comments = appendComments($_REQUEST['notes'], 'notes');
  220. //while editing the supplier id is passed.
  221. $purchase_quote_data['supplier_id'] = $_REQUEST['supplier_name'];
  222. $where_clause_quote = array('id'=>$id);
  223. $this->Request_quote_master->update($where_clause_quote,$purchase_quote_data,array('notes'=>$comments));
  224. }
  225. else if ($oper=='del'){
  226. $idAraay = $_REQUEST['id'];
  227. foreach($idAraay as $tempId){
  228. $where_clause_quote = array('id'=>$tempId);
  229. $this->Request_quote_master->update($where_clause_quote,array('status'=>'cancelled'));
  230. $where_clause_quote_item = array('rfq_id'=>$tempId);
  231. $this->Request_quote_item->update($where_clause_quote_item,array('status'=>'cancelled'));
  232. }
  233. }
  234. $this->db->trans_complete();
  235. }
  236. function addRFQItemBulk (){
  237. //$id = $_REQUEST['quoteId'];
  238. $data= $_REQUEST['data'];
  239. $purchase_quote_data['rfq_id'] = $_REQUEST['rfq'];
  240. $this->db->trans_start();
  241. foreach($data as $itemDetails){
  242. $purchase_quote_data['product_id'] = $itemDetails['id'];
  243. $purchase_quote_data['requested_quantity'] = $itemDetails['quantity'];
  244. $this->createRFQItem($purchase_quote_data);
  245. }
  246. $this->db->trans_complete();
  247. }
  248. function addRFQItem (){
  249. //$id = $_REQUEST['quoteId'];
  250. $product_id = $_REQUEST['productid'];
  251. $purchase_quote_data['rfq_id'] = $_REQUEST['rfq'];
  252. $purchase_quote_data['product_id'] = $product_id;
  253. $needed_by = $_REQUEST['needeedByDate'];
  254. if (!empty($needed_by)){
  255. $dateObj = DateTime::createFromFormat('d/m/Y', $needed_by);
  256. log_message('debug','converted item date '.$dateObj->format('Y-m-d'));
  257. $purchase_quote_data['needed_by_date'] = $dateObj->format('Y-m-d');
  258. }
  259. $purchase_quote_data['requested_quantity'] = $_REQUEST['quantity'];
  260. $purchase_quote_data['expected_price'] = $_REQUEST['exprice'];
  261. $purchase_quote_data['estimated_value'] = $_REQUEST['quantity']*$_REQUEST['exprice'];
  262. $purchase_quote_data['comments'] = $_REQUEST['descItem'];
  263. $id = $this->createRFQItem($purchase_quote_data);
  264. }
  265. //business logic
  266. function createRFQItem($purchase_quote_data){
  267. $this->db->trans_start();
  268. /* insert into quote item */
  269. $this->Request_quote_item->insert($purchase_quote_data);
  270. log_message('debug','insert statement ='.$this->db->last_query());
  271. $id = $this->db->insert_id();
  272. /* end insert */
  273. /* update reference number in quote item */
  274. $where_clause = array('id'=>$id);
  275. $this->Request_quote_item->update($where_clause, array('reference' => 10000000 + $id));
  276. /* end update */
  277. /* update estimated value in quote master */
  278. $rfq_id = $purchase_quote_data['rfq_id'];
  279. $quote_details=$this->Request_quote_master->getById($rfq_id);
  280. $estimated_value = $quote_details->estimated_value + $purchase_quote_data['estimated_value'];
  281. $upd_data['estimated_value'] = $estimated_value;
  282. if ($quote_details->status==self::DRAFT){
  283. //if the status is draft that means previously there was no items and for the first time we are adding items.
  284. // so we set the status as open
  285. $upd_data['status'] = self::OPEN;
  286. }
  287. $this->Request_quote_master->update(array('id'=>$rfq_id),$upd_data);
  288. log_message('debug','update statement ='.$this->db->last_query());
  289. /* end update estimated value in quote master */
  290. $this->db->trans_complete();
  291. return $id;
  292. }
  293. function modifyRFQItem (){
  294. $id = $_REQUEST['line_id'];
  295. $item_details=$this->Request_quote_item->getById($id);
  296. $rfq_id = $item_details->rfq_id;
  297. $current_est_value = $item_details->estimated_value;
  298. $oper = $_REQUEST['oper'];
  299. $this->db->trans_start();
  300. if ($oper=='edit'){
  301. if (!empty($_REQUEST['needed_by_date'])){
  302. $dateObj = DateTime::createFromFormat('d/m/Y', $_REQUEST['needed_by_date']);
  303. log_message('debug','converted item date '.$dateObj->format('Y-m-d'));
  304. $purchase_quote_data['needed_by_date'] = $dateObj->format('Y-m-d');
  305. }
  306. $purchase_quote_data['requested_quantity'] = $_REQUEST['quantity'];
  307. $purchase_quote_data['expected_price'] = $_REQUEST['expected_price'];
  308. $comments = appendComments($_REQUEST['comments'], 'comments');
  309. $purchase_quote_data['estimated_value'] = $_REQUEST['requested_quantity']*$_REQUEST['expected_price'];
  310. $where_clause = array('id'=>$id);
  311. $this->Request_quote_item->update($where_clause,$purchase_quote_data,array('comments'=>$comments));
  312. $quote_details=$this->Request_quote_master->getById($rfq_id);
  313. $estimated_value = $quote_details->estimated_value - $current_est_value + $purchase_quote_data['estimated_value'];
  314. $this->Request_quote_master->update(array('id'=>$rfq_id),array('estimated_value'=>$estimated_value));
  315. }
  316. else if ($oper=='del'){
  317. $quote_details=$this->Request_quote_master->getById($rfq_id);
  318. $estimated_value = $quote_details->estimated_value - $item_details->estimated_value;
  319. $this->Request_quote_master->update(array('id'=>$rfq_id),array('estimated_value'=>$estimated_value));
  320. $where_clause = array('id'=>$id);
  321. $this->Request_quote_item->update($where_clause,array('status'=>'cancelled'));
  322. }
  323. $this->db->trans_complete();
  324. }
  325. function populateRFQItems(){
  326. $quoteid = $_REQUEST['quoteId'];
  327. if (!empty($quoteid)){
  328. $quotedata = array();
  329. $where = array('rfq_id' => $quoteid );
  330. $griddata= populateGridCommon('Request_quote_item',$where);
  331. $dbrows = $griddata['db_data'];
  332. $data = $griddata['grid_metadata'];
  333. foreach ($dbrows as $dbrow){
  334. array_push($quotedata, array('id'=> $dbrow['id'],'dprow' => array($dbrow['name'],$dbrow['requested_quantity'],$dbrow['needed_by_date'],$dbrow['expected_price'],$dbrow['estimated_value'],$dbrow['comments'])));
  335. }
  336. $data['quoteitemdata'] = $quotedata;
  337. echo json_encode($data);
  338. }
  339. }
  340. function generateQuoteFromRFQ(){
  341. $ids= $_REQUEST['ids'];
  342. foreach($ids as $id){
  343. log_message('debug',' id '.$id);
  344. $rfq_details=$this->Request_quote_master->getById($id,null,true);
  345. if ($rfq_details['status']==self::OPEN) {
  346. $this->db->trans_start();
  347. //$this->Request_quote_master->update(array('id'=>$id),array('approved_by'=>$this->user->person_id));
  348. $this->generateQuoteInternal($rfq_details);
  349. $this->db->trans_complete();
  350. }
  351. }
  352. }
  353. function getCommentsForEntity(){
  354. $entity = $_REQUEST['entity'];
  355. $id = $_REQUEST['id'];
  356. if ($entity == 'rfq'){
  357. $model = 'Request_quote_master';
  358. $col_array = array('notes','approval_notes');
  359. }
  360. else if ($entity == 'quote'){
  361. $model = 'Purchase_quote_master';
  362. $col_array = array('notes','approval_notes');
  363. }
  364. else if ($entity == 'order'){
  365. $model = 'Purchase_order_master';
  366. $col_array = array('receiving_notes');
  367. }
  368. else if ($entity == 'receipt_item'){
  369. $model = 'Receipt_item';
  370. $col_array = array('receiving_notes','pp_approval_notes');
  371. }
  372. if (!empty($id)){
  373. $details=$this->$model->getById($id,$col_array,true);
  374. echo json_encode($details);
  375. }
  376. }
  377. function addCommentsForEntity(){
  378. $entity = $_REQUEST['entity'];
  379. $id = $_REQUEST['id'];
  380. $note_type = $_REQUEST['note_type'];
  381. // if ($entity == 'rfq'){
  382. // $model = 'Request_quote_master';
  383. // $col_array = array('notes','approval_notes');
  384. // }
  385. // else if ($entity == 'quote'){
  386. // $model = 'Purchase_quote_master';
  387. // $col_array = array('notes','approval_notes');
  388. // }
  389. // else if ($entity == 'order'){
  390. // $model = 'Purchase_order_master';
  391. // $col_array = array('receiving_notes');
  392. //
  393. // }
  394. if ($entity == 'receipt_item'){
  395. $model = 'Receipt_item';
  396. if ($note_type=='pp_approval'){
  397. $comments = appendComments($_REQUEST['notes'], 'pp_approval_notes');
  398. }
  399. }
  400. if (!empty($id)){
  401. $this->$model->update($id,null,array('pp_approval_notes'=>$comments));
  402. }
  403. }
  404. function generatePOFromQuote(){
  405. $ids= $_REQUEST['ids'];
  406. foreach($ids as $id){
  407. log_message('debug',' id '.$id);
  408. $quote_details=$this->Purchase_quote_master->getById($id,null,true);
  409. if ($quote_details['status']==self::OPEN ||$quote_details['status']==self::WAITING_FOR_APPROVAL ) {
  410. $this->db->trans_start();
  411. $this->Purchase_quote_master->update(array('id'=>$id),array('approved_by'=>$this->user->person_id));
  412. $this->generatePOInternal($quote_details);
  413. $this->db->trans_complete();
  414. }
  415. }
  416. }
  417. /* PURCHASE QUOTE GRID END*/
  418. /* APPROVAL GRUD START */
  419. function approveOrReject(){
  420. $id= $_REQUEST['quoteId'];
  421. $action= $_REQUEST['action'];
  422. $entity= $_REQUEST['entity'];
  423. if ($entity=='quote'){
  424. $model = 'Purchase_quote_master';
  425. $itemModel= 'Purchase_quote_item';
  426. $parent_id = 'quote_id';
  427. }
  428. else if ($entity=='rfq'){
  429. $model = 'Request_quote_master';
  430. $itemModel= 'Request_quote_item';
  431. $parent_id = 'rfq_id';
  432. }
  433. log_message('debug',' id '.$id);
  434. log_message('debug',' action '.$action);
  435. $quote_details=$this->$model->getById($id,null,true);
  436. if ($quote_details['status']==self::WAITING_FOR_APPROVAL ) {
  437. if ($action == 'approve'){
  438. $this->db->trans_start();
  439. if ($entity=='quote'){
  440. $this->$model->update(array('id'=>$id),array('approved_by'=>$this->user->person_id,'approval_notes'=>$_REQUEST['quote_approval_notes']));
  441. $this->generatePOInternal($quote_details);
  442. }
  443. else if ($entity=='rfq'){
  444. $this->$model->update(array('id'=>$id),array('approved_by'=>$this->user->person_id,'approval_notes'=>$_REQUEST['quote_approval_notes'],'status'=>self::SUBMITTEDTOQUOTE));
  445. $this->generateQuoteInternal($quote_details);
  446. }
  447. $this->db->trans_complete();
  448. }
  449. else if ($action == 'reject'){
  450. $this->db->trans_start();
  451. $this->$model->update(array('id'=>$id),array('status'=>self::REJECTED,'approved_by'=>$this->user->person_id,'approval_notes'=>$_REQUEST['quote_approval_notes']));
  452. $item_details=$this->$itemModel->getByQuoteId($id);
  453. foreach ($item_details as $item_data){
  454. $this->$itemModel->update(array('id'=>$item_data['id']),array('status'=>self::REJECTED));
  455. }
  456. $this->db->trans_complete();
  457. }
  458. }
  459. }
  460. function approveQuotesInBulk(){
  461. $ids= $_REQUEST['ids'];
  462. // $this->db->trans_start();
  463. foreach($ids as $id){
  464. $quote_details=$this->Request_quote_master->getById($id,null,true);
  465. if ($quote_details['status']==self::OPEN ||$quote_details['status']==self::WAITING_FOR_APPROVAL ) {
  466. $this->db->trans_start();
  467. $this->Request_quote_master->update(array('id'=>$id),array('approved_by'=>$this->user->person_id,'status'=>self::SUBMITTEDTOQUOTE));
  468. $this->generateQuoteInternal($quote_details);
  469. $this->db->trans_complete();
  470. }
  471. }
  472. //$this->db->trans_complete();
  473. }
  474. /* APPROVAL GRID END */
  475. /* REJECTED GRID START*/
  476. function reopen (){
  477. $ids= $_REQUEST['ids'];
  478. $entity= $_REQUEST['entity'];
  479. if ($entity=='quote'){
  480. $model = 'Purchase_quote_master';
  481. $itemModel= 'Purchase_quote_item';
  482. $parent_id = 'quote_id';
  483. }
  484. else if ($entity=='rfq'){
  485. $model = 'Request_quote_master';
  486. $itemModel= 'Request_quote_item';
  487. $parent_id = 'rfq_id';
  488. }
  489. foreach($ids as $id){
  490. log_message('debug',' id '.$id);
  491. $quote_details=$this->$model->getById($id,null,true);
  492. if ($quote_details['status']==self::REJECTED ) {
  493. $this->db->trans_start();
  494. $this->$model->update(array('id'=>$id),array('status'=>self::OPEN,'last_updated_by'=>$this->user->person_id));
  495. $item_details=$this->$itemModel->getByQuoteId($id);
  496. foreach ($item_details as $item_data){
  497. $this->$itemModel->update(array('id'=>$item_data['id']),array('status'=>self::OPEN));
  498. }
  499. $this->db->trans_complete();
  500. }
  501. }
  502. }
  503. /* REJECTED GRID END*/
  504. /* COMMON FUNCTIONS FOR RFQ GRIDS */
  505. private function generatePOInternal($quote_details){
  506. $id=$quote_details['id'];
  507. $order_data['owner_id'] = $quote_details['owner_id'];
  508. $order_data['needed_by_date'] = $quote_details['needed_by_date'];
  509. $order_data['generated_by'] = $this->user->person_id;
  510. $order_data['quote_id'] = $quote_details['id'];
  511. log_message('debug',json_encode($order_data));
  512. $order_id = $this->createOrder($order_data);
  513. log_message('debug',$this->db->last_query());
  514. $this->Purchase_quote_master->update(array('id'=>$id),array('status'=>self::ORDERED,'order_id'=>$order_id));
  515. log_message('debug',$this->db->last_query());
  516. //now update line item status
  517. $item_details=$this->Purchase_quote_item->getByQuoteId($id);
  518. foreach ($item_details as $item_data){
  519. if ($item_data['status'] == self::OPEN || $item_data['status'] == self::WAITING_FOR_APPROVAL){
  520. $order_item_data['name'] = $item_data['name'];
  521. $order_item_data['sku'] = $item_data['sku'];
  522. $order_item_data['product_id'] = $item_data['product_id'];
  523. $order_item_data['estimated_value'] = $item_data['estimated_value'];
  524. $order_item_data['needed_by_date'] = $item_data['needed_by_date'];
  525. $order_item_data['quoted_quantity'] = $item_data['quoted_quantity'];
  526. //repeatative ..as of now as no difference between quoted and ordered quantity
  527. $order_item_data['ordered_quantity'] = $item_data['quoted_quantity'];
  528. $order_item_data['expected_price'] = $item_data['expected_price'];
  529. $order_item_data['order_id'] =$order_id;
  530. $order_item_data['quote_line_id'] = $item_data['id'];
  531. $order_line_id = $this->createOrderItem($order_item_data);
  532. $this->Purchase_quote_item->update(array('id'=>$item_data['id']),array('status'=>self::ORDERED,'order_line_id'=>$order_line_id));
  533. }
  534. }
  535. }
  536. public function createOrder($purchase_quote_data){
  537. $this->db->trans_start();
  538. $this->Purchase_order_master->insert($purchase_quote_data);
  539. $id = $this->db->insert_id();
  540. $where_clause = array('id'=>$id);
  541. $this->Purchase_order_master->update($where_clause, array('reference' => 10000000 + $id));
  542. $this->db->trans_complete();
  543. return $id;
  544. }
  545. /*end quote */
  546. /* RECEIVING GRID START */
  547. function populatePOToReceive(){
  548. // $person_id = $this->user->person_id;
  549. //$in_where=array('field_name'=>'status','value_array'=>array(self::OPEN,self::RECEIVING,self::RECEIVED)) ;
  550. $this->populatePOInternal();
  551. }
  552. /* RECEIVING GRID END */
  553. function populatePayments(){
  554. $invoiceId = $_REQUEST['invoiceId'];
  555. $orderId = $_REQUEST['orderId'];
  556. $type = $_REQUEST['type'];
  557. if (!empty($invoiceId) || !empty($orderId)){
  558. $paymentdata = array();
  559. if (!empty($invoiceId)){
  560. $where['invoice_id'] = $invoiceId;
  561. }
  562. if (!empty($orderId)){
  563. $where['order_id'] = $orderId;
  564. }
  565. if (!empty($type)){
  566. $where['payment_type'] = $type;
  567. }
  568. $griddata= populateGridCommon('Outgoing_payment',$where);
  569. $dbrows = $griddata['db_data'];
  570. $data = $griddata['grid_metadata'];
  571. foreach ($dbrows as $dbrow){
  572. $isAdvance = 'No';
  573. if ($dbrow['parent_id'] != 0){
  574. // we have advance payment
  575. $isAdvance = 'Yes';
  576. }
  577. array_push($paymentdata, array('id'=> $dbrow['id'],'dprow' => array($dbrow['payment_reference'],$dbrow['payment_mode'],$dbrow['payment_type'],$dbrow['amount'],$dbrow['comments'],$isAdvance,$dbrow['parent_id'])));
  578. }
  579. $data['paymentdata'] = $paymentdata;
  580. echo json_encode($data);
  581. }
  582. }
  583. function populateReceipts(){
  584. $where = array();
  585. $in_where = array();
  586. $status = $_REQUEST['_status'];
  587. //default status
  588. if (empty($status)){
  589. //$where['invoice_id'] = $invoice_id;
  590. $in_where=array('field_name'=>'status','value_array'=>array(self::READYTOINVOICEMEMO)) ;
  591. }
  592. setOwnerStatusCommon($where,$in_where);
  593. $page = $_REQUEST['page'];
  594. $limit = $_REQUEST['rows'];
  595. $sidx = $_REQUEST['sidx'];
  596. $sord = $_REQUEST['sord'];
  597. $searchOn = $_REQUEST['_search'];
  598. if (!empty($person_id)){
  599. $where['owner_id'] = $person_id;
  600. }
  601. //standard response parameters
  602. $quotedata = array();
  603. $count = $this->Receipt_master->totalNoOfRows($where,null,null,$in_where);
  604. if( $count > 0 && $limit > 0) {
  605. $total_pages = ceil($count/$limit);
  606. } else {
  607. $total_pages = 0;
  608. }
  609. if ($page > $total_pages) $page=$total_pages;
  610. $start = $limit*$page - $limit;
  611. // if for some reasons start position is negative set it to 0
  612. // typical case is that the user type 0 for the requested page
  613. if($start <0) $start = 0;
  614. $clauses = array('orderBy'=>$sidx,'orderDir'=>$sord,'startLimit'=>$start,'limit'=>$limit);
  615. $data['total'] = $total_pages;
  616. $data['page'] = $page;
  617. $data['records'] = $count;
  618. if($searchOn=='true') {
  619. $filters = json_decode($_REQUEST['filters'],true);
  620. $groupOp = $filters['groupOp'];
  621. $rules = $filters['rules'];
  622. $like_condition = array();
  623. foreach ($rules as $rule){
  624. $field = $rule['field'];
  625. $op= $rule['op'];
  626. $input = $rule['data'];
  627. if ($field == 'order_reference' ){
  628. $orderDetails = $this->Purchase_order_master->getGridByReference($input);
  629. $like_condition['order_id'] = $orderDetails['id'];
  630. }
  631. else if ($field == 'quote_reference'){
  632. $orderDetails = $this->Purchase_order_master->getGridByQuoteReference($input);
  633. $like_condition['order_id'] = $orderDetails['id'];
  634. }
  635. else if ($field == 'supplier_name'){
  636. $supplierDetails = $this->Supplier->getByName($input);
  637. $like_condition['supplier_id'] = $supplierDetails['id'];
  638. }
  639. else {
  640. $like_condition[$field] = trim($input);
  641. }
  642. }
  643. $quotes = $this->Receipt_master->getAll(false,$where,null,$clauses,$like_condition,$in_where);
  644. }
  645. else {
  646. $quotes = $this->Receipt_master->getAll(false,$where,null,null,null,$in_where);
  647. }
  648. foreach ($quotes as $dp){
  649. $orderDetails = $this->Purchase_order_master->getGridById($dp['order_id']);
  650. array_push($quotedata, array('id'=> $dp['id'],'dprow' => array($dp['reference'],$dp['supplier_receipt_number'],$orderDetails['supplier_name'],$dp['status'],$dp['order_id'],$orderDetails['reference'],$orderDetails['quote_reference'],$dp['owner_name'],$dp['owner_id'],$dp['approved_by_name'],$dp['approved_by'])));
  651. }
  652. $data['quotedata'] = $quotedata;
  653. echo json_encode($data);
  654. }
  655. function populatePOInternal(){
  656. $where = array();
  657. $in_where = array();
  658. setOwnerStatusCommon($where,$in_where);
  659. //standard response parameters
  660. $orderdata = array();
  661. $griddata= populateGridCommon('Purchase_order_master',$where,null,$in_where);
  662. $dbrows = $griddata['db_data'];
  663. $data = $griddata['grid_metadata'];
  664. foreach ($dbrows as $dbrow){
  665. array_push($orderdata, array('id'=> $dbrow['id'],'dprow' => array($dbrow['reference'],$dbrow['quote_reference'],$dbrow['supplier_name'],$dbrow['estimated_value'],$dbrow['owner_id'],$dbrow['status'],$dbrow['raised_by_name'],$dbrow['order_generated_by_name'],$dbrow['owner_name'],$dbrow['needed_by_date'],$dbrow['received_by_name'],$dbrow['approved_by_name'],$dbrow['supplier_id'],$dbrow['warehouse_id'],$dbrow['received_by'])));
  666. }
  667. $data['orderdata'] = $orderdata;
  668. echo json_encode($data);
  669. }
  670. function validateReceiving(){
  671. $order_id = $_REQUEST['order_id'];
  672. // this note is for order
  673. $receiving_notes = $_REQUEST['receiving_notes'];
  674. //check the no of order line items with status receiving.
  675. $totalNoOfRowsByOrderIdReceived = $this->Purchase_order_item->totalNoOfRows(array('order_id'=>$order_id,'status'=>self::RECEIVED));
  676. $totalNoOfRowsByOrderIdTotal = $this->Purchase_order_item->totalNoOfRows(array('order_id'=>$order_id));
  677. if ($totalNoOfRowsByOrderIdReceived == $totalNoOfRowsByOrderIdTotal){
  678. // all the order lines are received
  679. $this->Purchase_order_master->update(array('id'=>$order_id,'status'=>self::RECEIVING),array('status'=>self::RECEIVED,'receiving_notes'=>$receiving_notes));
  680. $this->Receipt_master->update(array('order_id'=>$order_id,'status'=>self::RECEIVING),array('status'=>self::RECEIVED));
  681. }
  682. else {
  683. $this->Purchase_order_master->update(array('id'=>$order_id,'status'=>self::RECEIVING),array('receiving_notes'=>$receiving_notes));
  684. }
  685. }
  686. function modifyOrder (){
  687. $id = $_REQUEST['id'];
  688. $oper = $_REQUEST['oper'];
  689. $this->db->trans_start();
  690. if ($oper=='edit'){
  691. //$dateObj = DateTime::createFromFormat('d/m/Y', $_REQUEST['needed_by_date']);
  692. //log_message('debug','converted item date '.$dateObj->format('Y-m-d'));
  693. $purchase_quote_data['needed_by_date'] = $_REQUEST['needed_by_date'];
  694. $purchase_quote_data['supplier_name'] = $_REQUEST['supplier_name'];
  695. $where_clause_quote = array('id'=>$id);
  696. $this->Purchase_quote_master->update($where_clause_quote,$purchase_quote_data);
  697. }
  698. else if ($oper=='del'){
  699. $idAraay = explode(",", $id);
  700. foreach($idAraay as $tempId){
  701. $where_clause_quote = array('id'=>$tempId);
  702. $this->Purchase_quote_master->update($where_clause_quote,array('status'=>'cancelled'));
  703. $where_clause_quote_item = array('quote_id'=>$tempId);
  704. $this->Purchase_quote_item->update($where_clause_quote_item,array('status'=>'cancelled'));
  705. }
  706. }
  707. $this->db->trans_complete();
  708. }
  709. function addOrderItem (){
  710. //$id = $_REQUEST['quoteId'];
  711. $product_id = $_REQUEST['productid'];
  712. $purchase_quote_data['quote_id'] = $_REQUEST['orderId'];
  713. $purchase_quote_data['product_id'] = $product_id;
  714. $dateObj = DateTime::createFromFormat('d/m/Y', $_REQUEST['needeedByDate']);
  715. log_message('debug','converted item date '.$dateObj->format('Y-m-d'));
  716. $purchase_quote_data['needed_by_date'] = $dateObj->format('Y-m-d');
  717. $purchase_quote_data['quoted_quantity'] = $_REQUEST['quantity'];
  718. $purchase_quote_data['expected_price'] = $_REQUEST['exprice'];
  719. $purchase_quote_data['estimated_value'] = $_REQUEST['quantity']*$_REQUEST['exprice'];
  720. $purchase_quote_data['comments'] = $_REQUEST['descItem'];
  721. $productDetails = $this->Product->getByProductId($product_id);
  722. $purchase_quote_data['sku'] = $productDetails->barcode;
  723. $purchase_quote_data['name'] = $productDetails->product_name;
  724. $id = $this->createQuoteItem($purchase_quote_data);
  725. }
  726. //business logic
  727. function createOrderItem($purchase_quote_data){
  728. $this->db->trans_start();
  729. /* insert into quote item */
  730. $this->Purchase_order_item->insert($purchase_quote_data);
  731. log_message('debug','insert statement ='.$this->db->last_query());
  732. $id = $this->db->insert_id();
  733. /* end insert */
  734. /* update reference number in quote item */
  735. $where_clause = array('id'=>$id);
  736. $this->Purchase_order_item->update($where_clause, array('reference' => 10000000 + $id));
  737. /* end update */
  738. $this->db->trans_complete();
  739. return $id;
  740. }
  741. function modifyOrderItem (){
  742. $id = $_REQUEST['id'];
  743. $item_details=$this->Purchase_quote_item->getById($id);
  744. $quote_id = $item_details->quote_id;
  745. $current_est_value = $item_details->estimated_value;
  746. $oper = $_REQUEST['oper'];
  747. $this->db->trans_start();
  748. if ($oper=='edit'){
  749. //$dateObj = DateTime::createFromFormat('d/m/Y', $_REQUEST['needed_by_date']);
  750. //log_message('debug','converted item date '.$dateObj->format('Y-m-d'));
  751. $purchase_quote_data['needed_by_date'] = $_REQUEST['needed_by_date'];
  752. $purchase_quote_data['quoted_quantity'] = $_REQUEST['quoted_quantity'];
  753. $purchase_quote_data['expected_price'] = $_REQUEST['expected_price'];
  754. $purchase_quote_data['comments'] = $_REQUEST['comments'];
  755. $purchase_quote_data['estimated_value'] = $_REQUEST['quoted_quantity']*$_REQUEST['expected_price'];
  756. $where_clause = array('id'=>$id);
  757. $this->Purchase_quote_item->update($where_clause,$purchase_quote_data);
  758. $quote_details=$this->Purchase_quote_master->getById($quote_id);
  759. $estimated_value = $quote_details->estimated_value - $current_est_value + $purchase_quote_data['estimated_value'];
  760. $this->Purchase_quote_master->update(array('id'=>$quote_id),array('estimated_value'=>$estimated_value));
  761. }
  762. else if ($oper=='del'){
  763. $quote_details=$this->Purchase_quote_master->getById($quote_id);
  764. $estimated_value = $quote_details->estimated_value - $item_details->estimated_value;
  765. $this->Purchase_quote_master->update(array('id'=>$quote_id),array('estimated_value'=>$estimated_value));
  766. $where_clause = array('id'=>$id);
  767. $this->Purchase_quote_item->update($where_clause,array('status'=>'cancelled'));
  768. }
  769. $this->db->trans_complete();
  770. }
  771. function populateOrderItems(){
  772. $orderid = $_REQUEST['orderId'];
  773. if (!empty($orderid)){
  774. $orderitemdata = array();
  775. $where = array('order_id' => $orderid );
  776. $griddata= populateGridCommon('Purchase_order_item',$where);
  777. $dbrows = $griddata['db_data'];
  778. $data = $griddata['grid_metadata'];
  779. foreach ($dbrows as $dbrow){
  780. array_push($orderitemdata, array('id'=> $dbrow['id'],'dprow' => array($dbrow['product_id'],$dbrow['name'],$dbrow['quoted_quantity'],$dbrow['received_quantity'],$dbrow['returned_quantity'],$dbrow['cnbd_quantity'],$dbrow['pp_quantity'],$dbrow['needed_by_date'],$dbrow['expected_price'],$dbrow['estimated_value'],$dbrow['received_value'],$dbrow['returned_value'],$dbrow['pp_value'],$dbrow['comments'])));
  781. }
  782. $data['orderitemdata'] = $orderitemdata;
  783. echo json_encode($data);
  784. }
  785. }
  786. function populateReceiptItems(){
  787. $receiptId = $_REQUEST['receiptId'];
  788. $orderLineId = $_REQUEST['orderLineId'];
  789. $receipt_line_value_in =array();
  790. $populateGrid= true;
  791. // as of now keep default oper as "receipt"
  792. $oper = "receipt";
  793. if (!empty($_REQUEST['oper'])){
  794. $oper = $_REQUEST['oper'];
  795. }
  796. $where = array();
  797. $in_where = array();
  798. setOwnerStatusCommon($where,$in_where);
  799. //standard response parameters
  800. $quotedata = array();
  801. if ($oper=="receipt"){
  802. $where['receipt_id'] = $receiptId ;
  803. }
  804. else if ($oper=="orderline"){
  805. $where['order_line_id'] = $orderLineId ;
  806. }
  807. // else if ($oper=="pp"){
  808. // $where= array(/*'pp_quantity >' => 0 ,*/'owner_id'=>$this->user->person_id,'status !='=>self::COMPLETE);
  809. // }
  810. else if ($oper=="pp_approve"){
  811. $where['pp_quantity >']= 0;
  812. $receipt_line_value_array = array_unique($this->Receipt_partpayment->getAll(false,array('status'=>self::WAITING_FOR_APPROVAL),array('receipt_line_id')));
  813. //dont fir the query if there is no pp item with wfa mode
  814. if (count($receipt_line_value_array)==0){
  815. $populateGrid = false;
  816. }
  817. foreach ($receipt_line_value_array as $value){
  818. array_push($receipt_line_value_in,$value['receipt_line_id']);
  819. }
  820. }
  821. if ($populateGrid===true){
  822. $griddata= populateGridCommon('Receipt_item',$where,null,array('field_name'=>'id','value_array'=>$receipt_line_value_in));
  823. $quotes = $griddata['db_data'];
  824. $data = $griddata['grid_metadata'];
  825. foreach ($quotes as $dp){
  826. $receipt_data = $this->Receipt_master->getById($dp['receipt_id'],array('supplier_receipt_number','reference','owner_id','owner_name'),true);
  827. $po_item_data = $this->Purchase_order_item->getById($dp['order_line_id'],array('reference','order_id'),true);
  828. //$test = $po_item_data['reference'];
  829. array_push($quotedata, array('id'=> $dp['id'],'dprow' => array($dp['reference'],$dp['batch_number']/*receipt line reference*/,$receipt_data['supplier_receipt_number'],$receipt_data['reference'],/*receipt reference*/
  830. $dp['receipt_id'],$po_item_data['order_id'], $dp['order_line_id'],$po_item_data['reference'],$dp['product_id'],$dp['name'],$dp['expiry_date'],$dp['vat'],$dp['ordered_quantity'],$dp['received_quantity'],$dp['received_value'],$dp['pp_quantity'],$dp['pp_value'],$dp['returned_quantity'],$dp['returned_value'],$dp['pp_status'],/*$dp['receiving_notes'],$dp['returned_notes'],*/$receipt_data['owner_name'],$receipt_data['owner_id'],$dp['approved_by_name'],$dp['approved_by'])));
  831. }
  832. }
  833. $data['receiptitemdata'] = $quotedata;
  834. echo json_encode($data);
  835. }
  836. function populatePartpaymentItems(){
  837. $ppdata = array();
  838. $receipt_line_id= $_REQUEST['receipt_line_id'];
  839. $where = array('receipt_line_id' => $receipt_line_id );
  840. $in_where['field_name'] = 'status' ;
  841. $in_where['value_array'] = $_REQUEST['_status'] ;
  842. $griddata= populateGridCommon('Receipt_partpayment',$where,array('id','receipt_id','receipt_line_id',
  843. 'pp_quantity','pp_value','pp_notes','status'),$in_where);
  844. $dbrows = $griddata['db_data'];
  845. $data = $griddata['grid_metadata'];
  846. foreach ($dbrows as $row){
  847. array_push($ppdata, array('id'=> $row['id'],'dprow' => array($row['receipt_id'],$row['receipt_line_id'],
  848. $row['pp_quantity'], $row['pp_value'],$row['pp_notes'],$row['status'])));
  849. }
  850. $data['ppdata'] = $ppdata;
  851. echo json_encode($data);
  852. }
  853. function getOrderDetails(){
  854. $orderId=$_REQUEST['orderId'];
  855. if (!empty($orderId)){
  856. $order = $this->Purchase_order_master->getGridById($orderId);
  857. echo json_encode($order);
  858. }
  859. }
  860. function getQuoteDetails(){
  861. $quoteId=$_REQUEST['quoteId'];
  862. if (!empty($quoteId)){
  863. $quote = $this->Purchase_quote_master->getGridById($quoteId);
  864. echo json_encode($quote);
  865. }
  866. }
  867. function getRFQDetails(){
  868. $quoteId=$_REQUEST['quoteId'];
  869. if (!empty($quoteId)){
  870. $quote = $this->Request_quote_master->getGridById($quoteId);
  871. echo json_encode($quote);
  872. }
  873. }
  874. function loadPOGrid()
  875. {
  876. $this->load->view("procurement/purchaseorder/purchase_order_grid",$data);
  877. }
  878. function loadOpenInvoicesGrid()
  879. {
  880. $this->load->view("procurement/invoice/purchase_invoice_grid",$data);
  881. }
  882. function loadFormFragment(){
  883. $this->load->view("procurement/purchaseorder/po_details",$data);
  884. }
  885. function loadNotesFragment(){
  886. $this->load->view("procurement/purchaseorder/po_notes",$data);
  887. }
  888. function loadWaitingForApprovalQuotesGrid (){
  889. $this->load->view("procurement/quote/approval/quote_approval_grid",$data);
  890. }
  891. function loadRejectedQuotesGrid (){
  892. $this->load->view("procurement/quote/rejected/quote_rejected_grid",$data);
  893. }
  894. function loadRejectedRFQGrid (){
  895. $this->load->view("procurement/quote/rejected/request_rejected_grid",$data);
  896. }
  897. function loadQuoteFormFragment(){
  898. $this->load->view("procurement/quote/approval/quote_details",$data);
  899. }
  900. function loadQuoteNotesFragment(){
  901. $this->load->view("procurement/quote/approval/quote_notes",$data);
  902. }
  903. function loadWaitingForApprovalRFQGrid (){
  904. $this->load->view("procurement/quote/approval/request_approval_grid",$data);
  905. }
  906. function loadWaitingForApprovalRFQFormFragment(){
  907. $this->load->view("procurement/quote/approval/request_details",$data);
  908. }
  909. function loadWaitingForApprovalRFQNotesFragment(){
  910. $this->lo

Large files files are truncated, but you can click here to view the full file