cmc-sales/controllers/shipments_controller.php
2011-09-27 13:47:36 +10:00

311 lines
6.6 KiB
PHP

<?php
class ShipmentsController extends AppController {
var $name = 'Shipments';
var $helpers = array('Html', 'Form');
var $paginate = array(
'contain' => false,
'limit' => 300,
'recursive' => 1
);
function index($type = 'all') {
$this->Shipment->recursive = 0;
switch($type) {
case 'all':
$conditions = array();
break;
case 'import':
$conditions = array('Shipment.type'=>1);
break;
case 'direct':
$conditions = array('Shipment.type'=>2);
break;
case 'export':
$conditions = array('Shipment.type'=>3);
break;
case 'local':
$conditions = array('Shipment.type'=>4);
break;
default:
}
$this->set('shipments', $this->paginate($conditions));
$this->set('type', $type);
$principles = $this->Shipment->Principle->find('list');
$freightForwarders = $this->Shipment->FreightForwarder->find('list');
$shipmentType = $this->typeOptions(false);
$shipmentTypeShort = $this->typeOptions(true);
$currencies = $this->Shipment->ShipmentInvoice->Currency->find('list', array('fields'=>array('Currency.id','Currency.iso4217')));
$principles = $this->Shipment->Principle->find('list');
$this->set(compact('principles', 'freightForwarders', 'shipmentType', 'shipmentTypeShort', 'currencies', 'principles'));
}
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid Shipment.', true));
$this->redirect(array('action'=>'index'));
}
$this->set('shipment', $this->Shipment->read(null, $id));
}
function add() {
$principles = $this->Shipment->Principle->find('list');
$freightForwarders = $this->Shipment->FreightForwarder->find('list');
$shipmentType = $this->typeOptions(false);
$shipmentTypeShort = $this->typeOptions(true);
$currencies = $this->Shipment->ShipmentInvoice->Currency->find('list');
$invoiceType = $this->invoiceTypeOptions();
$this->set('yesNo', array(0=>'No', 1=>'Yes'));
$this->set(compact('principles', 'freightForwarders', 'shipmentType', 'shipmentTypeShort', 'currencies','invoiceType', 'yesNo'));
}
function edit($id = null) {
if(isset($id)) {
if (empty($this->data)) {
$this->data = $this->Shipment->read(null, $id);
$address = $this->Shipment->Address->find('first', array('conditions'=>array('Address.id'=>$this->data['Shipment']['address_id']),'recursive'=>0));
$principles = $this->Shipment->Principle->find('list');
$freightForwarders = $this->Shipment->FreightForwarder->find('list');
$shipmentType = $this->typeOptions(false);
$shipmentTypeShort = $this->typeOptions(true);
$currencies = $this->Shipment->ShipmentInvoice->Currency->find('list');
$invoiceType = $this->invoiceTypeOptions();
$this->set('yesNo', array(0=>'No', 1=>'Yes'));
$this->set(compact('principles', 'freightForwarders', 'shipmentType', 'shipmentTypeShort', 'currencies','invoiceType', 'yesNo', 'address'));
}
}
}
function ajax_edit() {
$this->layout = 'ajax';
if(!empty($this->data)) {
//$this->Shipment->create($this->data);
$shipment_id = $this->data['Shipment']['id'];
$this->Shipment->Box->deleteAll(array('shipment_id'=>$shipment_id));
$this->Shipment->ShipmentInvoice->deleteAll(array('shipment_id'=>$shipment_id));
if ($this->Shipment->saveAll($this->data)) {
$this->Shipment->Box->updateCounterCache(array('shipment_id'=>$shipment_id));
$this->Shipment->ShipmentInvoice->updateCounterCache(array('shipment_id'=>$shipment_id));
/*$boxIDs = array();
foreach($this->data['Box'] as $box) {
$boxIDs[] = $box['id'];
}
$invoiceIDs = array();
foreach($this->data['ShipmentInvoice'] as $si) {
$invoiceIDs[] = $si['id'];
}
print_r($boxIDs);*/
if($this->data['Shipment']['type'] > 1) {
$jobIDs = array();
foreach($this->data['Job'] as $job) {
foreach($job as $index => $id) {
$jobIDs[] = $id;
}
}
$this->Shipment->Job->updateAll(array('Job.all_sent' => 1), array('Job.id'=>$jobIDs));
}
//print_r($this->data);
echo "SUCCESS";
}
else {
echo "FAILURE";
}
}
else {
echo "NO-DATA";
}
}
function ajax_add() {
$this->layout = 'ajax';
if(!empty($this->data)) {
$this->Shipment->create($this->data);
if ($this->Shipment->saveAll($this->data)) {
echo "SUCCESS";
}
else {
echo "FAILURE";
}
}
else {
echo "NO-DATA";
}
}
function markJobsSent() {
}
/* Moved the Views for Adding/Editing to elements. Called from the Index.
*
* function add($action = 'add') {
$principles = $this->Shipment->Principle->find('list');
$freightForwarders = $this->Shipment->FreightForwarder->find('list');
$shipmentType = $this->typeOptions();
$this->set(compact('principles', 'freightForwarders', 'shipmentType'));
}*/
function invoiceTypeOptions() {
$options = array('', 'Freight','Customs','Principle');
return $options;
}
function typeOptions($short = false) {
if($short == false) {
$options = array('','Import: Principle to CMC','Direct: Principle to Customer','Export','Local');
}
else {
$options = array('','Import','Direct','Export','Local');
}
return $options;
}
function delete($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid id for Shipment', true));
$this->redirect(array('action'=>'index'));
}
if ($this->Shipment->del($id)) {
$this->Session->setFlash(__('Shipment deleted', true));
$this->redirect(array('action'=>'index'));
}
}
function reports() {
$invoices = $this->Shipment->ShipmentInvoice->find('all',array('conditions'=>array('ShipmentInvoice.deferred_gst > 0')));
/*foreach($shipments as $shipment) {
foreach($shipment['ShipmentInvoice'] as $si) {
if($si['deferred_gst'] == 1) {
echo $si['deferred_gst_amount']."\n";
}
}
}*/
$Fyears = $this->getFinancialYears($invoices);
$this->set('Fyears',$Fyears);
$this->set('invoices', $invoices);
}
function getFinancialYears($shipments) {
$Fyears = array();
foreach($shipments as $shipment) {
$time = strtotime($shipment['Shipment']['created']);
$year = date('Y', $time); //ISO-8601 year number
$startFYunix = $this->getFirstDayFY($year); //1 July of $year.
if($time < $startFYunix) { //Date is before 1 July $year.
$lastYear = $year - 1;
if(!isset($Fyears[$year])) {
$Fyears[$year] = 1;
}
else {
$Fyears[$year]++;
}
}
else { //Date is After 1 July $year.
$nextYear = $year + 1;
if(!isset($Fyears[$nextYear])) {
$Fyears[$nextYear] = 1;
}
else {
$Fyears[$nextYear]++;
}
}
}
krsort($Fyears); //Sort the array in reverse order, most recent to oldest.
return $Fyears;
}
}
?>