Exemple #1
0
 public static function getAccountCentreId($_glaccount_id = '', $_glcentre_id = '', &$errors = array())
 {
     if (!empty($_glaccount_id) && !empty($_glcentre_id)) {
         $accountcentre = new GLAccountCentre();
         $accountcentre->loadBy(array('glaccount_id', 'glcentre_id'), array($_glaccount_id, $_glcentre_id));
         if ($accountcentre->isLoaded()) {
             return $accountcentre->id;
         } else {
             $errors[] = 'Invalid Account/Centre combination';
         }
         return '';
     }
 }
 public function save()
 {
     if (!$this->CheckParams($this->modeltype)) {
         sendBack();
     }
     $flash = Flash::Instance();
     $errors = array();
     $db = DB::Instance();
     $db->StartTrans();
     $current_accounts = array();
     if (isset($this->_data[$this->modeltype]['id'])) {
         $centre = $this->_templateobject;
         $centre->load($this->_data[$this->modeltype]['id']);
         if ($centre->isLoaded()) {
             foreach ($centre->accounts as $accountcentre) {
                 // Delete any existing entries not in the supplied list
                 if (!empty($this->_data[$this->modeltype]['account_id']) && in_array($accountcentre->glaccount_id, $this->_data[$this->modeltype]['account_id'])) {
                     $current_accounts[$accountcentre->glaccount_id] = $accountcentre->glaccount_id;
                 } elseif (!$accountcentre->delete(null, $errors)) {
                     $errors[] = 'Failed to update Account Centre Reference ' . $accountcentre->glaccount;
                 }
             }
         }
     }
     if (count($errors) === 0 && parent::save_model($this->modeltype)) {
         if (isset($this->_data[$this->modeltype]['account_id'])) {
             foreach ($this->_data[$this->modeltype]['account_id'] as $account_id) {
                 if (!key_exists($account_id, $current_accounts)) {
                     $data = array();
                     $data['glaccount_id'] = $account_id;
                     $data['glcentre_id'] = $this->saved_model->id;
                     $accountcentre = GLAccountCentre::Factory($data, $errors, 'GLAccountCentre');
                     if ($accountcentre) {
                         $accountcentre->save();
                         if (!$accountcentre) {
                             $errors[] = 'Failed to save reference to Account';
                             break;
                         }
                     } else {
                         $errors[] = 'Failed to save reference to Account';
                         break;
                     }
                 }
             }
         }
         if (count($errors) == 0) {
             $db->CompleteTrans();
             sendTo($this->name, 'index', $this->_modules);
         }
     } else {
         $errors[] = 'Failed to save GL Centre';
     }
     $db->FailTrans();
     $flash->addErrors($errors);
     $this->refresh();
 }
Exemple #3
0
 public static function Factory(SInvoice $header, $line_data, &$errors)
 {
     if (empty($line_data['invoice_id'])) {
         $line_data['invoice_id'] = $header->id;
     }
     if (empty($line_data['line_number'])) {
         $line_data['line_number'] = $header->getNextLineNumber();
     }
     if ($line_data['sales_qty'] > 0 && $line_data['sales_price'] > 0) {
         // do nothing
     } else {
         $errors[] = 'Zero quantity or net value';
         return false;
     }
     $line_data['invoice_id'] = $header->id;
     if (!isset($line_data['move_stock'])) {
         $line_data['move_stock'] = 'f';
     }
     if ($line_data['productline_id'] == -1) {
         $line_data['productline_id'] = '';
         $line_data['stitem_id'] = '';
     } elseif ($line_data['productline_id'] > 0) {
         $productline = DataObjectFactory::Factory('SOProductline');
         $productline->load($line_data['productline_id']);
         if ($productline && $productline->isLoaded()) {
             $productlineheader = $productline->product_detail;
             $line_data['stitem_id'] = $productlineheader->stitem_id;
             $line_data['item_description'] = $productlineheader->stitem;
         } else {
             $line_data['stitem_id'] = '';
         }
         if (!empty($line_data['stitem_id'])) {
             $stitem = DataObjectFactory::Factory('STitem');
             $stitem->load($line_data['stitem_id']);
             if ($stitem) {
                 $line_data['item_description'] = $stitem->getIdentifierValue();
                 $line_data['stuom_id'] = $stitem->uom_id;
             }
         }
     }
     if (empty($line_data['description'])) {
         $line_data['description'] = $line_data['item_description'];
     }
     $line_data['tax_status_id'] = $header->tax_status_id;
     $line_data['currency_id'] = $header->currency_id;
     $line_data['rate'] = $header->rate;
     $line_data['twin_currency_id'] = $header->twin_currency_id;
     $line_data['twin_rate'] = $header->twin_rate;
     $line_data['settlement_discount'] = $header->payment_term->calcSettlementDiscount($line_data['net_value']);
     $line_data['glaccount_centre_id'] = GLAccountCentre::getAccountCentreId($line_data['glaccount_id'], $line_data['glcentre_id'], $errors);
     return parent::makeLine($line_data, 'SInvoiceLine', $errors);
 }
Exemple #4
0
 public static function Factory(SOrder $header, $line_data, &$errors)
 {
     if (empty($line_data['order_id'])) {
         $line_data['order_id'] = $header->id;
     }
     if (empty($line_data['line_number'])) {
         $line_data['line_number'] = $header->getNextLineNumber();
     }
     $line_data['item_description'] = $line_data['description'];
     if ($line_data['productline_id'] == -1) {
         $line_data['productline_id'] = '';
         $line_data['stitem_id'] = '';
     } else {
         $productline = DataObjectFactory::Factory('SOProductline');
         $productline->load($line_data['productline_id']);
         if ($productline->isLoaded()) {
             $productlineheader = $productline->product_detail;
             if (is_null($productlineheader->stitem_id)) {
                 $line_data['item_description'] = $productline->getDescription();
                 $line_data['stitem_id'] = '';
             } else {
                 $line_data['item_description'] = $productlineheader->stitem;
                 $line_data['stitem_id'] = $productlineheader->stitem_id;
             }
             if (empty($line_data['price'])) {
                 $line_data['price'] = $productline->getPrice('', '', $productline->slmaster_id);
             }
             if (empty($line_data['glaccount_id'])) {
                 $line_data['glaccount_id'] = $productline->glaccount_id;
             }
             if (empty($line_data['glcentre_id'])) {
                 $line_data['glcentre_id'] = $productline->glcentre_id;
             }
             if (empty($line_data['stuom_id'])) {
                 $line_data['stuom_id'] = $productlineheader->stuom_id;
             }
             if (empty($line_data['tax_rate_id'])) {
                 $line_data['tax_rate_id'] = $productlineheader->tax_rate_id;
             }
         }
         // Check if glaccount_centre_id exists - can be any value including null
         if (!array_key_exists('glaccount_centre_id', $line_data)) {
             $line_data['glaccount_centre_id'] = GLAccountCentre::getAccountCentreId($line_data['glaccount_id'], $line_data['glcentre_id'], $errors);
         }
         if (empty($line_data['net_value'])) {
             $line_data['net_value'] = bcmul($line_data['price'], $line_data['revised_qty']);
         }
     }
     if ($line_data['revised_qty'] > 0 && $line_data['price'] > 0) {
         if (empty($line_data['id'])) {
             // New Line
             $line_data['order_qty'] = $line_data['os_qty'] = $line_data['revised_qty'];
             $line_data['status'] = 'N';
         } else {
             // Amended Line
             if ($line_data['status'] == 'N') {
                 $line_data['os_qty'] = $line_data['revised_qty'];
             }
         }
     } else {
         $errors[] = 'Zero quantity or net value';
     }
     if (count($errors) > 0) {
         return false;
     }
     if (empty($line_data['description'])) {
         $line_data['description'] = $line_data['item_description'];
     }
     $line_data['line_discount'] = 0;
     $line_data['currency_id'] = $header->currency_id;
     $line_data['rate'] = $header->rate;
     $line_data['twin_currency_id'] = $header->twin_currency_id;
     $line_data['twin_rate'] = $header->twin_rate;
     $line_data['base_net_value'] = bcadd(round(bcdiv($line_data['net_value'], $line_data['rate'], 4), 2), 0);
     $line_data['twin_net_value'] = bcadd(round(bcmul($line_data['base_net_value'], $line_data['twin_rate'], 4), 2), 0);
     if (empty($line_data['due_delivery_date'])) {
         $line_data['due_delivery_date'] = un_fix_date($header->due_date);
     }
     if (empty($line_data['due_despatch_date'])) {
         $line_data['due_despatch_date'] = un_fix_date($header->despatch_date);
     }
     return parent::Factory($line_data, $errors, 'SOrderLine');
 }
 public function savePayments()
 {
     $flash = Flash::Instance();
     $errors = array();
     $data = $this->_data['PeriodicPayment'];
     $db = DB::Instance();
     $db->StartTrans();
     foreach ($data as $id => $set) {
         if (isset($set['pay']) || isset($set['skip'])) {
             $pp = new PeriodicPayment();
             $pp->load($id);
             if (isset($set['pay'])) {
                 //	Create payment record array
                 $data = $pp->makePaymentTransaction();
                 $data['description'] = $set['description'];
                 $data['ext_reference'] = $set['ext_reference'];
                 if (isset($set['next_due_date'])) {
                     $data['transaction_date'] = $set['next_due_date'];
                 }
                 if (isset($set['net_value'])) {
                     $data['net_value'] = $set['net_value'];
                     $data['tax_value'] = $set['tax_value'];
                     $data['value'] = bcadd($data['net_value'], $data['tax_value']);
                 }
                 if (isset($set['gross_value'])) {
                     $data['tax_value'] = 0.0;
                     $data['net_value'] = $data['value'] = $set['gross_value'];
                 }
                 if ($pp->source == 'CR' || $pp->source == 'CP') {
                     if ($pp->source == 'CP') {
                         $data['net_value'] = bcmul($data['net_value'], -1);
                         $data['tax_value'] = bcmul($data['tax_value'], -1);
                         $data['value'] = bcmul($data['value'], -1);
                     }
                     CBTransaction::saveCashPayment($data, $errors);
                 } else {
                     if ($pp->source == 'PP') {
                         $payment = new PLTransaction();
                     } else {
                         $payment = new SLTransaction();
                     }
                     $payment->saveTransaction($data, $errors);
                 }
                 //	Update Periodic Payment record
                 $pp->current++;
                 if (!is_null($pp->end_date) && $pp->next_due_date > $pp->end_date) {
                     $pp->status = 'S';
                 }
                 if (!is_null($pp->occurs) && $pp->current >= $pp->occurs) {
                     $pp->status = 'S';
                 }
                 if ($pp->variable == 't' && $pp->write_variance == 't') {
                     $pp->net_value = $data['net_value'];
                     $pp->tax_value = $data['tax_value'];
                     $pp->gross_value = $data['value'];
                 }
                 if ($pp->status == 'S') {
                     $pp->glaccount_centre_id = null;
                 } else {
                     $pp->glaccount_centre_id = GLAccountCentre::getAccountCentreId($pp->glaccount_id, $pp->glcentre_id, $errors);
                 }
             }
             // Update the date if skipping or paying
             $pp->nextDate();
             if (count($errors) > 0 || !$pp->save()) {
                 $errors[] = 'Failed to update Periodic Payment details';
                 break;
             }
         }
     }
     if (count($errors) == 0 && $db->completeTrans()) {
         $flash->addMessage('Payments processed OK');
         sendTo($_SESSION['refererPage']['controller'], $_SESSION['refererPage']['action'], $_SESSION['refererPage']['modules'], isset($_SESSION['refererPage']['other']) ? $_SESSION['refererPage']['other'] : null);
     } else {
         $db->FailTrans();
         $db->completeTrans();
         $flash->addErrors($errors);
         $flash->addError('Failed to make payments');
         $this->refresh();
     }
 }
Exemple #6
0
 public static function Factory(PInvoice $header, $line_data, &$errors)
 {
     if (empty($line_data['invoice_id'])) {
         $line_data['invoice_id'] = $header->id;
     }
     if (empty($line_data['line_number'])) {
         $line_data['line_number'] = $header->getNextLineNumber();
     }
     if ($line_data['net_value'] > 0) {
         // do nothing
     } else {
         $errors[] = 'Zero net value';
         return FALSE;
     }
     $line_data['invoice_id'] = $header->id;
     if (empty($line_data['description'])) {
         $line_data['description'] = $line_data['item_description'];
     }
     $line_data['tax_status_id'] = $header->tax_status_id;
     $line_data['currency_id'] = $header->currency_id;
     $line_data['rate'] = $header->rate;
     $line_data['twin_currency_id'] = $header->twin_currency_id;
     $line_data['twin_rate'] = $header->twin_rate;
     $line_data['glaccount_centre_id'] = GLAccountCentre::getAccountCentreId($line_data['glaccount_id'], $line_data['glcentre_id'], $errors);
     return parent::makeLine($line_data, 'PInvoiceLine', $errors);
 }
 public function save()
 {
     if (!$this->CheckParams($this->modeltype)) {
         sendBack();
     }
     $flash = Flash::Instance();
     $errors = array();
     $data = $this->_data[$this->modeltype];
     if (empty($data['id'])) {
         $data['next_due_date'] = $data['start_date'];
         $data['status'] = 'A';
     }
     if ($data['status'] == 'A' && !empty($data['glaccount_id']) && !empty($data['glcentre_id']) && $data['source'] != 'SR' && $data['source'] != 'PP') {
         $data['glaccount_centre_id'] = GLAccountCentre::getAccountCentreId($data['glaccount_id'], $data['glcentre_id'], $errors);
     } else {
         unset($data['glaccount_id']);
         unset($data['glcentre_id']);
         unset($data['glaccount_centre_id']);
     }
     if (empty($data['net_value']) && empty($data['tax_value']) && empty($data['gross_value'])) {
         $flash->addError('No value entered');
     } else {
         if (empty($data['net_value']) || $data['net_value'] == 0) {
             $data['net_value'] = $data['gross_value'];
         }
         if (empty($data['tax_value'])) {
             $data['tax_value'] = 0;
         }
         if (empty($data['gross_value']) || $data['gross_value'] == 0) {
             $data['gross_value'] = bcadd($data['net_value'], $data['tax_value']);
         }
     }
     if (count($errors) === 0 && parent::save_model($this->modeltype, $data)) {
         sendTo($this->name, 'index', $this->_modules);
     } else {
         $flash->addErrors($errors);
         $this->refresh();
     }
 }