public function index()
 {
     App::import('Model', 'QuoteManager.Quote');
     $quoteModel = new Quote();
     $quoteDataList = $quoteModel->find('all', array('fields' => array('quote_number', 'status'), 'conditions' => array('Quote.vid' => null, 'Quote.created_by' => $this->loginUser['id'])));
     App::import('Model', 'WorkOrderManager.WorkOrder');
     $workOrderModel = new WorkOrder();
     $workOrderDataList = $workOrderModel->find('all', array('fields' => array('work_order_number', 'status'), 'conditions' => array('WorkOrder.created_by' => $this->loginUser['id'])));
     App::import('Model', 'PurchaseOrderManager.PurchaseOrder');
     $purchaseOrderModel = new PurchaseOrder();
     $purchaseOrderDataList = $purchaseOrderModel->find('all', array('fields' => array('purchase_order_num', 'received'), 'conditions' => array('PurchaseOrder.created_by' => $this->loginUser['id'])));
     $this->set(compact('quoteDataList', 'workOrderDataList', 'purchaseOrderDataList'));
 }
 /**
  * add method
  *
  * @return void
  */
 public function add($id = null, $type = null)
 {
     $data = array();
     $itemData = array();
     if ($type == 'Purchase Order') {
         App::import('Model', 'PurchaseOrderManager.PurchaseOrder');
         $purchaseOrder = new PurchaseOrder();
         $purchaseOrder->recursive = 0;
         $data = $purchaseOrder->find('first', array('conditions' => array('PurchaseOrder.id' => $id)));
         $itemData = $this->QuoteItem->listOfPoItem(null, $id);
     } elseif ($type == 'Quote') {
         App::import('Model', 'QuoteManager.Quote');
         $quote = new Quote();
         $quote->recursive = 1;
         $data = $quote->find('first', array('conditions' => array('Quote.id' => $id)));
         $itemData = $this->QuoteItem->ListQuoteItems($id);
         //      debug($itemData);
         $itemData = $this->QuoteItem->AdjustPOItem($itemData);
         $itemData['main_list'] = $itemData['name_list'];
         unset($itemData['name_list']);
         $itemData['qty_list'] = $itemData['quantity_list'];
         unset($itemData['quantity_list']);
         //      debug($data);
         //      debug($itemData);
         //
         //      exit;
     }
     $invoice_no = $this->QuoteItem->auto_generate_number('Invoice');
     $invoiceFormatData = $this->InvoiceItem->formatInvoiceData($invoice_no, $type, $id, $data, $itemData);
     $invoiceCreate = $this->InvoiceItem->createInvoice($invoiceFormatData);
     if ($invoiceCreate) {
         $this->Session->setFlash(__('The invoice has been saved'));
         $this->redirect(array('action' => 'detail', $invoiceCreate['Invoice']['id']));
     } else {
         $this->Session->setFlash(__('The invoice could not be saved. Please, try again.'));
     }
     $this->set(compact('invoiceStatuses', 'type'));
 }
 function auto_generate_number($type)
 {
     $value = "";
     if ($type == "Quote") {
         App::uses('Quote', 'QuoteManager.Model');
         $quoteModel = new Quote();
         $quotes = $quoteModel->find('all', array('fields' => array('Quote.id,Quote.quote_number')));
         if ($quotes) {
             $quote_number = (int) $quotes[count($quotes) - 1]['Quote']['quote_number'];
             $cnt = count($quotes);
             $max = 0;
             for ($i = 1; $i <= $cnt; $i++) {
                 $num = (int) $quotes[$i - 1]['Quote']['quote_number'];
                 if ($max < $num) {
                     $max = $num;
                 }
             }
             $quote_number = explode("-", $quote_number);
             $length = strlen($quote_number[0] + 1) == strlen($quote_number[0]) ? strlen($quote_number[0]) : strlen($quote_number[0] + 1);
             for ($i = $length; $i < 6; $i++) {
                 $value .= '0';
             }
             //$value.=$quote_number[0] + 1;
             $value .= $max + 1;
         } else {
             $value = "000001";
         }
     } elseif ($type == "Work Order") {
         App::uses('WorkOrder', 'WorkOrderManager.Model');
         $woModel = new WorkOrder();
         $wos = $woModel->find('all', array('fields' => array('WorkOrder.id,work_order_number')));
         if ($wos) {
             $wo_number = (int) $wos[count($wos) - 1]['WorkOrder']['work_order_number'];
             $length = strlen($wo_number + 1) == strlen($wo_number) ? strlen($wo_number) : strlen($wo_number + 1);
             for ($i = $length; $i < 6; $i++) {
                 $value .= '0';
             }
             $value .= $wo_number + 1;
         } else {
             $value = "000001";
         }
     } elseif ($type == "Purchase Order") {
         App::uses('PurchaseOrder', 'PurchaseOrderManager.Model');
         $poModel = new PurchaseOrder();
         $pos = $poModel->find('all', array('fields' => array('PurchaseOrder.id,purchase_order_num')));
         if ($pos) {
             $po_number = (int) $pos[count($pos) - 1]['PurchaseOrder']['purchase_order_num'];
             $length = strlen($po_number + 1) == strlen($po_number) ? strlen($po_number) : strlen($po_number + 1);
             for ($i = $length; $i < 6; $i++) {
                 $value .= '0';
             }
             $value .= $po_number + 1;
         } else {
             $value = "000001";
         }
     } elseif ($type == "Invoice") {
         App::uses('Invoice', 'Invoice.Model');
         $invoiceModel = new Invoice();
         $invoice = $invoiceModel->find('all', array('fields' => array('Invoice.id,invoice_no')));
         if ($invoice) {
             $invoice_number = (int) $invoice[count($invoice) - 1]['Invoice']['invoice_no'];
             $length = strlen($invoice_number + 1) == strlen($invoice_number) ? strlen($invoice_number) : strlen($invoice_number + 1);
             for ($i = $length; $i < 6; $i++) {
                 $value .= '0';
             }
             $value .= $invoice_number + 1;
         } else {
             $value = "000001";
         }
     }
     return $value;
 }
 private function create_po($data, $work_id)
 {
     $quote_id = $data['WorkOrder']['quote_id'];
     App::uses('Quote', 'QuoteManager.Model');
     App::uses('PurchaseOrder', 'PurchaseOrderManager.Model');
     $quote = new Quote();
     $purchaseOrder = new PurchaseOrder();
     $quote->recursive = 0;
     $quoteInfo = $quote->find('all', array('conditions' => array('Quote.id' => $quote_id)));
     //set po id
     $po_id = $purchaseOrder->find('list', array('fields' => array('id')));
     $tmp_po_id = 0;
     if (count($po_id) > 0) {
         $tmp_po_id = array_pop($po_id) + 1;
     } else {
         $tmp_po_id = 1;
     }
     $all_items = $this->QuoteItem->ListQuoteItems($quote_id);
     //    debug($all_items);
     //    exit;
     $all_items = $this->QuoteItem->AdjustPOItem($all_items);
     $quantity_list = $all_items['quantity_list'];
     $price_list = $all_items['price_list'];
     $title_list = $all_items['title_list'];
     $name_list = $all_items['name_list'];
     $supplier_list = $this->QuoteItem->SupplierAndItemInfo();
     $total_cost = 0.0;
     $index1 = 0;
     $item_have = false;
     foreach ($supplier_list as $key => $supplier) {
         $po_data['PurchaseOrder'] = array();
         $po_data['PurchaseOrderItem'] = array();
         //debug($supplier);
         $index2 = 0;
         $item_total_cost = 0.0;
         foreach ($supplier['item'] as $item_key => $item) {
             if ($quantity_list && is_array($quantity_list)) {
                 if (array_key_exists($item, $quantity_list)) {
                     //debug($quantity_list[$item]);
                     $po_data['PurchaseOrderItem'][$index2]['quantity'] = $quantity_list[$item];
                     $po_data['PurchaseOrderItem'][$index2]['code'] = $item;
                     $item_total_cost += $price_list[$item] * $quantity_list[$item];
                     //debug($item);
                     //debug($quantity_list[$item]);
                     //debug($price_list[$item]);
                     //debug($item_total_cost);
                     $item_arr = explode('|', $item);
                     $item_id = $item_arr[1] == 'item' ? $item_arr[0] : 0;
                     $cabite_id = $item_arr[1] == 'cabinet' ? $item_arr[0] : 0;
                     $door_id = $item_arr[1] == 'door' || $item_arr[1] == 'drawer' || $item_arr[1] == 'wall_door' ? $item_arr[0] : 0;
                     $po_data['PurchaseOrderItem'][$index2]['item_id'] = $item_id;
                     $po_data['PurchaseOrderItem'][$index2]['cabinet_id'] = $cabite_id;
                     $po_data['PurchaseOrderItem'][$index2]['door_id'] = $door_id;
                     $index2++;
                     //set item in supplier
                     $item_have = true;
                 }
             }
         }
         if ($item_have) {
             $value = $this->QuoteItem->auto_generate_number('Purchase Order');
             $po_data['PurchaseOrder']['id'] = $tmp_po_id;
             $po_data['PurchaseOrder']['supplier_id'] = $key;
             $po_data['PurchaseOrder']['purchase_order_num'] = $value;
             $po_data['PurchaseOrder']['quote_id'] = $quote_id;
             $po_data['PurchaseOrder']['work_order_id'] = $work_id;
             $po_data['PurchaseOrder']['tax_gst'] = $supplier['gst'];
             $po_data['PurchaseOrder']['order_subtotal'] = $item_total_cost;
             $gst_val = $item_total_cost * $supplier['gst'] / 100;
             $pst_val = $item_total_cost * $supplier['pst'] / 100;
             $po_data['PurchaseOrder']['total_amount'] = $item_total_cost + $pst_val + $gst_val;
             $po_data['PurchaseOrder']['tax_pst'] = $supplier['pst'];
             $item_have = false;
             //debug($po_data);
             $purchaseOrder->save($po_data);
             //        $email = new CakeEmail();
             //        $email->config('gmail');
             //        $email->from(array('*****@*****.**' => 'Zenliving'));
             //        $email->to('*****@*****.**');
             //        $email->subject('Zenliving');
             //        $email->send('My message');
             $to = $supplier['email'];
             $subject = "Test mail";
             $message = "Hello! This is a test message.";
             $from = "*****@*****.**";
             $headers = "From:" . $from;
             mail($to, $subject, $message, $headers);
             //echo "Mail Sent.";
             $tmp_po_id++;
         }
     }
 }