PageRenderTime 46ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/opensourcepos/application/controllers/procurement.php

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

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