Пример #1
0
 public function printDespatchNote($status = 'generate')
 {
     $despatch_number = $this->_data['despatch_number'];
     $despatch_date = $this->_data['despatch_date'];
     $order_id = $this->_data['order_id'];
     // build options array
     $options = array('type' => array('pdf' => '', 'xml' => ''), 'output' => array('print' => '', 'save' => '', 'email' => '', 'view' => ''), 'filename' => 'DN' . $despatch_number, 'report' => 'DespatchNote');
     if (strtolower($status) == "dialog") {
         return $options;
     }
     // load the model
     $despatchnote = new SODespatchLineCollection($this->_templateobject);
     $sh = new SearchHandler($despatchnote, false);
     $sh->addConstraint(new Constraint('despatch_number', '=', $despatch_number));
     $despatchnote->load($sh);
     $order = DataObjectFactory::Factory('SOrder');
     $order->load($order_id);
     // get the company address
     $company_address = array('name' => $this->getCompanyName());
     $company_address += $this->formatAddress($this->getCompanyAddress());
     $extra['company_address'] = $company_address;
     // get the company details
     $extra['company_details']['tel'] = 'Tel  : ' . $this->getContactDetails('T');
     $extra['company_details']['fax'] = 'Fax  : ' . $this->getContactDetails('F');
     $extra['company_details']['email'] = 'email: ' . $this->getContactDetails('E');
     // get the despatch location
     $extra['despatch_location'] = implode(',', $order->despatch_from->rules_list('from_location'));
     // get customer account number
     $customer = DataObjectFactory::Factory('SLCustomer');
     $customer->load($order->slmaster_id);
     $customername = $customer->name;
     $extra['account_number'] = $customer->accountnumber();
     // get customer address
     $customer_address = array('title' => 'Customer Address:', 'name' => $customername);
     $customer_address += $this->formatAddress($customer->getBillingAddress());
     $extra['customer_address'] = $customer_address;
     // get delivery address
     $delivery_address = array('title' => 'Delivery Address:', 'name' => $customername);
     $delivery_address += $this->formatAddress($customer->getDeliveryAddress($order->del_address_id));
     $extra['delivery_address'] = $delivery_address;
     // generate the xml and add it to the options array
     $options['xmlSource'] = $this->generate_xml(array('model' => array($order, $despatchnote), 'extra' => $extra));
     // ATTN: We're handling multiple models in this example, but we also
     // want to handle an individual relationship_whitelist for both models
     // execute the print output function, echo the returned json for jquery
     $json_response = $this->generate_output($this->_data['print'], $options);
     // if($response->status===true) {
     //
     // }
     // now we've done our checks, output the original JSON for jQuery to use
     // echo the response if we're using ajax, return the response otherwise
     if (isset($this->_data['ajax'])) {
         echo $json_response;
     } else {
         return $json_response;
     }
     exit;
 }
Пример #2
0
 private function createPostInvoice($sorder, &$errors)
 {
     if ($sorder->customerdetails->accountStopped()) {
         $errors[] = 'Cannot create invoice, customer account stopped';
         return FALSE;
     }
     set_time_limit(0);
     $linestatuses = $sorder->getLineStatuses();
     $linestatus = $linestatuses['count'];
     if (!$sorder->someLinesDespatched($linestatus) && !$sorder->someLinesPicked($linestatus)) {
         $errors[] = 'No lines picked or despatched';
         return FALSE;
     }
     $sinvoicelines = array();
     $db = DB::Instance();
     $db->startTrans();
     $sodespatchlines = array();
     $create_sodespatchlines = array();
     $latest_despatch_date = 0;
     $sorderline = DataObjectFactory::Factory('SOrderLine');
     $sorderlines = new SOrderLineCollection($sorderline);
     $sh = new SearchHandler($sorderlines, FALSE);
     $sh->addConstraint(new Constraint('order_id', '=', $sorder->id));
     $statuses = array($db->qstr($sorderline->despatchStatus()), $db->qstr($sorderline->pickedStatus()));
     $sh->addConstraint(new Constraint('status', 'in', '(' . implode(',', $statuses) . ')'));
     $sorderlines->load($sh);
     $sodespatchline = DataObjectFactory::Factory('SODespatchLine');
     foreach ($sorderlines as $sorderline) {
         $sinvoiceline = array();
         $sodespatchlines[$sorderline->id]['id'] = $sorderline->id;
         $sodespatchlines[$sorderline->id]['line_number'] = $sorderline->line_number;
         if ($sorderline->status == $sorderline->pickedStatus()) {
             $sinvoiceline['move_stock'] = 't';
             $sorderline->del_qty = $sorderline->revised_qty;
             $sorderline->actual_despatch_date = fix_date(date(DATE_FORMAT));
             if (!is_null($sorderline->stitem_id)) {
                 $create_sodespatchlines[$sorder->id][$sorderline->id] = SODespatchLine::makeline($sorder, $sorderline, $errors);
                 $create_sodespatchlines[$sorder->id][$sorderline->id]['status'] = $sodespatchline->despatchStatus();
             }
             $sodespatchlines[$sorderline->id]['fields'] = array('status', 'del_qty', 'actual_despatch_date');
             $sodespatchlines[$sorderline->id]['values'] = array($sorderline->invoicedStatus(), $sorderline->del_qty, $sorderline->actual_despatch_date);
         } else {
             $sodespatchlines[$sorderline->id]['fields'] = array('status');
             $sodespatchlines[$sorderline->id]['values'] = array($sorderline->invoicedStatus());
         }
         foreach ($sorderline->getFields() as $key => $value) {
             $sinvoiceline[$key] = $sorderline->{$key};
         }
         foreach ($sorderline->audit_fields as $field) {
             unset($sinvoiceline[$field]);
         }
         if ($sorderline->actual_despatch_date > $latest_despatch_date) {
             $latest_despatch_date = $sorderline->actual_despatch_date;
         }
         $sinvoiceline['sales_order_id'] = $sorderline->order_id;
         $sinvoiceline['order_line_id'] = $sorderline->id;
         $sinvoiceline['sales_qty'] = $sorderline->del_qty;
         $sinvoiceline['sales_price'] = $sorderline->price;
         $sinvoiceline['net_value'] = bcmul($sorderline->price, $sorderline->del_qty);
         $sinvoiceline['glaccount_centre_id'] = null;
         $sinvoiceline['id'] = '';
         $sinvoicelines[] = $sinvoiceline;
     }
     if (count($errors) == 0 && count($create_sodespatchlines) > 0 && !SODespatchLine::createDespatchNote($create_sodespatchlines, $errors)) {
         $errors[] = 'Error creating Despatch Note';
     }
     foreach ($sodespatchlines as $id => $data) {
         $data['fields'][] = 'glaccount_centre_id';
         $data['values'][] = 'null';
         $result = $this->_uses['SOrderLine']->update($id, $data['fields'], $data['values']);
         if (!$result) {
             $errors['id' . $id] = 'Failed to update order line ' . $data['line_number'] . ' : ' . $db->errorMsg();
         }
     }
     if (count($errors) == 0) {
         // Check line statuses and update header status accordingly
         if (!$sorder->save()) {
             $errors[] = 'Failed to update order header ' . $db->ErrorMsg();
         }
     }
     if (count($sinvoicelines) == 0) {
         $errors[] = 'Failed to create invoice lines';
     }
     // Save the Invoice Header if no errors
     if (count($errors) == 0) {
         $sinvoiceheader = array();
         foreach ($sorder->getFields() as $key => $value) {
             $sinvoiceheader[$key] = $sorder->{$key};
         }
         foreach ($sorder->audit_fields as $field) {
             unset($sinvoiceheader[$field]);
         }
         $sinvoiceheader['id'] = '';
         $sinvoiceheader['sales_order_id'] = $sorder->id;
         $sinvoiceheader['sales_order_number'] = $sorder->order_number;
         $sinvoiceheader['despatch_date'] = un_fix_date($latest_despatch_date);
         $sinvoiceheader['transaction_type'] = 'I';
         $sinvoiceheader['invoice_date'] = un_fix_date($latest_despatch_date);
         $sinvoice = SInvoice::Factory($sinvoiceheader, $errors);
         if ($sinvoice) {
             $result = $sinvoice->save();
             if (!$result) {
                 $errors[] = 'Failed to create invoice';
             }
         }
     }
     // Save the Invoice Lines if no errors
     if (count($errors) == 0) {
         $result = FALSE;
         foreach ($sinvoicelines as $line_data) {
             $sinvoiceline = SInvoiceLine::Factory($sinvoice, $line_data, $errors);
             if ($sinvoiceline) {
                 $result = $sinvoiceline->save();
             }
             if (!$result) {
                 $errors[] = 'Failed to create invoice line ' . $db->ErrorMsg();
                 break;
             }
         }
         // Now save the header again to update the totals
         $result = $sinvoice->save();
         if (!$result) {
             $errors[] = 'Failed to update invoice totals';
             $errors[] = $db->ErrorMsg();
         }
     }
     // Post the Invoice if no errors
     if (count($errors) == 0) {
         $result = $sinvoice->post($errors);
         if (!$result) {
             $errors[] = 'Failed to post invoice';
         }
         foreach ($sodespatchlines as $id => $data) {
             $sodespatchlines = new SODespatchLineCollection($sodespatchline);
             $sh = new SearchHandler($sodespatchlines, FALSE);
             $sh->addConstraint(new Constraint('orderline_id', '=', $id));
             $sh->addConstraint(new Constraint('status', '=', $sodespatchline->despatchStatus()));
             $sh->addConstraint(new Constraint('invoice_id', 'is', 'NULL'));
             if ($sodespatchlines->update(array('invoice_id', 'invoice_number'), array($sinvoice->id, $sinvoice->invoice_number), $sh) === false) {
                 $errors[] = 'Error updating despatch lines : ' . $db->ErrorMsg();
             }
         }
     }
     if (count($errors) > 0) {
         $db->FailTrans();
         $result = FALSE;
     } else {
         // Everything is OK so return the newly created Invoice object
         $result = $sinvoice;
     }
     $db->CompleteTrans();
     return $result;
 }