2010-07-07 23:18:04 -07:00
< ? php
class DocumentsController extends AppController {
2013-04-13 03:01:47 -07:00
var $name = 'Documents' ;
var $helpers = array ( 'Html' , 'Form' , 'Number' , 'Decimal' );
var $paginate = array (
2013-04-29 06:37:27 -07:00
'contain' => true ,
2013-04-13 03:01:47 -07:00
'limit' => 200 ,
'order' => array ( 'Document.id' => 'desc' )
);
var $components = array ( 'Email' );
function index () {
$this -> Document -> recursive = 0 ;
2016-05-17 05:48:43 -07:00
//$this->set('documents', $this->paginate());
2019-04-17 03:44:20 -07:00
//print_r$($this->params["named"]["page"]);
2016-05-17 06:04:00 -07:00
2019-07-02 02:54:22 -07:00
$query = " SELECT id,type,created,user_id,doc_page_count, pdf_filename,pdf_created_at, pdf_created_by_user_id FROM documents As Document ORDER BY id DESC LIMIT 1000; " ;
2019-03-18 03:50:18 -07:00
$this -> set ( 'documents' , $this -> Document -> query ( $query ));
2016-05-17 05:48:43 -07:00
2013-04-13 03:01:47 -07:00
$this -> set ( 'users' , $this -> Document -> User -> getUsersList ());
}
function view ( $id = null ) {
if ( ! $id ) {
$this -> Session -> setFlash ( __ ( 'Invalid Document.' , true ));
$this -> redirect ( array ( 'action' => 'index' ));
}
2013-06-23 01:31:21 -07:00
//$document = $this->Document->read(null,$id);
$document = $this -> Document -> find ( 'first' , array ( 'conditions' => array ( 'Document.id' => $id ), 'recursive' => 1 ));
2013-04-13 03:01:47 -07:00
$this -> set ( 'document' , $document );
$this -> data = $document ;
$docType = $this -> Document -> getDocType ( $document );
$this -> set ( 'docType' , $docType );
$this -> set ( 'docTypeFullName' , $this -> Document -> getDocFullName ( $docType ));
$this -> set ( 'users' , $this -> Document -> User -> getUsersList ());
$this -> set ( 'currency' , $this -> Document -> getCurrency ( $document ));
$enquiry = $this -> Document -> getEnquiry ( $document );
$this -> set ( 'enquiry' , $enquiry );
2013-04-22 05:57:57 -07:00
if ( $enquiry ) {
$this -> set ( 'jobs' , $this -> Document -> Invoice -> Job -> find ( 'list' ,
array ( 'conditions' => array ( 'Job.customer_id' => $enquiry [ 'Enquiry' ][ 'customer_id' ]), 'recursive' => 0 , 'order' => 'id DESC' )));
}
2013-04-13 03:01:47 -07:00
$customerAddresses = $this -> Document -> User -> Enquiry -> BillingAddress -> find ( 'all' , array ( 'conditions' => array ( 'BillingAddress.customer_id' => $enquiry [ 'Enquiry' ][ 'customer_id' ])));
/* (2013). This STILL feels like an ugly hack. But it gets the desired effect and it will do for now. Screw it. */
if ( isset ( $customerAddresses [ 0 ][ 'BillingAddress' ])) {
foreach ( $customerAddresses as $address ) {
$i = $address [ 'BillingAddress' ][ 'id' ];
$billing_address_list [ $i ] = $address [ 'BillingAddress' ][ 'address' ] . '<br>' . $address [ 'BillingAddress' ][ 'city' ] . ' ' . $address [ 'State' ][ 'name' ] . ' ' . $address [ 'BillingAddress' ][ 'postcode' ] . '<br>' . up ( $address [ 'Country' ][ 'name' ]) . '<br><br>' ;
}
$this -> set ( 'billing_addresses_list' , $billing_address_list );
$this -> set ( 'shipping_addresses_list' , $billing_address_list );
}
else {
$this -> set ( 'billing_addresses_list' , 'No Addresses exist for this Customer. Please add one' );
$this -> set ( 'shipping_addresses_list' , 'No Addresses exist for this Customer. Please add one' );
}
$this -> set ( 'attachments' , $this -> Document -> DocumentAttachment -> find ( 'all' , array ( 'conditions' => array ( 'document_id' => $id ))));
$this -> set ( 'principles' , $this -> Document -> LineItem -> Product -> Principle -> find ( 'list' ));
switch ( $docType ) {
case 'quote' :
$docTypeElement = 'document_quote_view' ;
break ;
case 'invoice' :
$docTypeElement = 'document_invoice_view' ;
$this -> set ( 'currencies' , $this -> Document -> Invoice -> Currency -> find ( 'list' ));
$this -> set ( 'invoice' , $this -> Document -> Invoice -> find ( 'first' , array ( 'conditions' => array ( 'Invoice.id' => $document [ 'Invoice' ][ 'id' ]))));
break ;
case 'purchaseOrder' :
$docTypeElement = 'document_purchase_order_view' ;
2013-04-22 05:57:57 -07:00
$this -> set ( 'currencies' , $this -> Document -> PurchaseOrder -> Currency -> find ( 'list' ));
2013-06-23 01:31:21 -07:00
$this -> set ( 'purchaseOrder' , $this -> Document -> PurchaseOrder -> find ( 'first' ,
array ( 'conditions' =>
array ( 'PurchaseOrder.id' => $document [ 'PurchaseOrder' ][ 'id' ])
)
));
2013-04-13 03:01:47 -07:00
break ;
case 'orderAck' :
$this -> set ( 'currencies' , $this -> Document -> OrderAcknowledgement -> Currency -> find ( 'list' ));
$this -> set ( 'job' , $this -> Document -> OrderAcknowledgement -> Job -> find ( 'first' , array ( 'conditions' => array ( 'Job.id' => $document [ 'OrderAcknowledgement' ][ 'job_id' ]))));
$docTypeElement = 'document_orderack_view' ;
2013-06-10 02:27:23 -07:00
break ;
2013-06-08 23:37:00 -07:00
case 'packingList' :
$docTypeElement = 'document_packinglist_view' ;
2013-06-10 02:27:23 -07:00
$this -> set ( 'currencies' , $this -> Document -> Invoice -> Currency -> find ( 'list' ));
$this -> set ( 'packing_list' , $this -> Document -> PackingList -> find ( 'first' , array ( 'conditions' => array ( 'PackingList.id' => $document [ 'PackingList' ][ 'id' ]))));
2013-06-08 23:37:00 -07:00
break ;
2013-04-13 03:01:47 -07:00
}
$this -> set ( 'docTypeElement' , $docTypeElement );
// $this->set('principles', $this->Document->LineItem->Product->Principle->find('list'));
}
function getProducts ( $principleID = null ) {
$this -> layout = 'ajax' ;
if ( $principleID == null ) {
echo " INVALID PRINCIPLE ID " ;
}
else {
$products = $this -> Document -> LineItem -> Product -> find ( 'list' , array ( 'conditions' => array ( 'Product.principle_id' => $principleID )));
$this -> set ( 'products' , $products );
}
}
/**
* Return a JSON object of the Product .
*/
function getProductDetails ( $productID = null ) {
$this -> layout = 'ajax' ;
if ( $productID == null ) {
echo " INVALID PRODUCT ID " ;
}
else {
$product = $this -> Document -> LineItem -> Product -> find ( 'first' , array ( 'conditions' => array ( 'Product.id' => $productID )));
$this -> set ( 'product' , $product );
}
}
function getAttachments ( $documentID ) {
$this -> layout = 'ajax' ;
if ( ! $documentID ) {
echo " FAIL " ;
}
else {
$this -> set ( 'attachments' , $this -> Document -> DocumentAttachment -> find ( 'all' , array ( 'conditions' => array ( 'document_id' => $documentID ))));
}
}
function getAttachmentsByPrinciple ( $principleID ) {
$this -> layout = 'ajax' ;
if ( ! $principleID ) {
echo " FAIL " ;
}
else {
$attachments = $this -> Document -> DocumentAttachment -> Attachment -> find ( 'all' , array ( 'conditions' => array ( 'principle_id' => $principleID , 'archived' => 0 )));
$this -> set ( 'attachments' , $attachments );
}
}
function saveAttachments () {
$this -> layout = 'ajax' ;
if ( $this -> data ) {
$attachments = array ();
foreach ( $this -> data [ 'DocumentAttachment' ][ 'attachment_id' ] as $i => $attachment_id ) {
$docID = $this -> data [ 'DocumentAttachment' ][ 'document_id' ];
$query = " DELETE FROM document_attachments WHERE document_id = { $docID } AND attachment_id = { $attachment_id } " ;
$this -> Document -> DocumentAttachment -> query ( $query );
$attachment [ $i ][ 'DocumentAttachment' ][ 'document_id' ] = $this -> data [ 'DocumentAttachment' ][ 'document_id' ];
$attachment [ $i ][ 'DocumentAttachment' ][ 'attachment_id' ] = $attachment_id ;
}
if ( $this -> Document -> DocumentAttachment -> saveAll ( $attachment )) {
echo " SUCCESS " ;
}
else {
echo " FAIL " ;
}
}
}
function removeAttachments () {
$this -> layout = 'ajax' ;
foreach ( $this -> data [ 'DocumentAttachment' ] as $attachment ) {
$this -> Document -> DocumentAttachment -> delete ( $attachment [ 'id' ]);
}
echo 'SUCCESS' ;
2013-03-28 18:38:50 -07:00
}
2012-11-18 12:19:55 -08:00
2013-04-13 03:01:47 -07:00
/**
* Create a New Document
*
*
2013-04-22 05:57:57 -07:00
* @ param string $type
* @ param int $id
* @ param int $jobid
* @ param int $source_document_id
2013-04-13 03:01:47 -07:00
*/
2013-04-22 05:57:57 -07:00
function newDocument ( $type = null , $enquiryid = null , $jobid = 0 , $source_document_id = null ) {
2013-04-13 03:01:47 -07:00
if ( ! $type ) {
$this -> Session -> setFlash ( 'Invalid Doc Type or ID' );
}
$this -> Document -> create ();
switch ( $type ) {
case " quote " :
$enquiry = $this -> Document -> Quote -> Enquiry -> findById ( $enquiryid );
2010-07-07 23:18:04 -07:00
2013-04-13 03:01:47 -07:00
if ( ! $enquiry ) {
$this -> Session -> setFlash ( 'Invalid Enquiry ID' );
$this -> redirect ( array ( 'action' => 'index' ));
return ;
}
2011-08-11 00:30:03 -07:00
2011-08-11 22:46:26 -07:00
2013-04-13 03:01:47 -07:00
$this -> data [ 'Quote' ][ 'enquiry_id' ] = $enquiryid ;
$number_of_revisions = $this -> Document -> Quote -> findCount ( 'Quote.enquiry_id =' . $enquiryid );
$this -> data [ 'Document' ][ 'revision' ] = $number_of_revisions ;
$this -> data [ 'Quote' ][ 'date_issued' ] = date ( 'Y-m-d' );
$this -> data [ 'Quote' ][ 'currency_id' ] = 2 ; //All new Quotes default to AUD.
2011-08-11 22:46:26 -07:00
2013-04-13 03:01:47 -07:00
$this -> data [ 'Document' ][ 'user_id' ] = $enquiry [ 'Enquiry' ][ 'user_id' ];
2011-08-11 22:46:26 -07:00
2013-04-13 03:01:47 -07:00
$this -> data [ 'Document' ][ 'type' ] = 'quote' ;
break ;
case " invoice " :
2011-08-11 22:46:26 -07:00
2013-04-13 03:01:47 -07:00
$invoice_number_offset = 4436 ; //What Invoice number we are up to. Starting at 4500 due to the data loss.
2011-08-11 22:46:26 -07:00
2013-04-13 03:01:47 -07:00
$number_of_invoices = $this -> Document -> Invoice -> findCount ();
2011-08-11 22:46:26 -07:00
2013-04-13 03:01:47 -07:00
$enquiry = $this -> Document -> Invoice -> Enquiry -> read ( null , $enquiryid );
if ( ! $enquiry ) {
$this -> Session -> setFlash ( 'Invalid Enquiry ID' );
$this -> redirect ( array ( 'action' => 'index' ));
return ;
}
2011-08-11 22:46:26 -07:00
2013-04-13 03:01:47 -07:00
$newInvoiceNumber = $invoice_number_offset + $number_of_invoices ;
$this -> data [ 'Invoice' ][ 'enquiry_id' ] = $enquiryid ;
$this -> data [ 'Invoice' ][ 'title' ] = " CMCIN " . $newInvoiceNumber ;
$this -> data [ 'Invoice' ][ 'job_id' ] = $jobid ;
$this -> data [ 'Invoice' ][ 'customer_id' ] = $enquiry [ 'Enquiry' ][ 'customer_id' ];
2011-08-11 22:46:26 -07:00
2013-04-13 03:01:47 -07:00
$this -> data [ 'Document' ][ 'user_id' ] = $enquiry [ 'Enquiry' ][ 'user_id' ];
//$number_of_revisions = $this->Document->Invoice->findCount('Invoice.enquiry_id ='. $enquiryid);
$this -> data [ 'Document' ][ 'revision' ] = 0 ; //No revisions for Invoices. I have decreed it!
$this -> data [ 'Document' ][ 'type' ] = 'invoice' ;
2011-08-11 22:46:26 -07:00
2013-04-13 03:01:47 -07:00
$this -> data [ 'Invoice' ][ 'issue_date' ] = date ( 'Y-m-d' ); //Argh. Quote model uses 'date_issed'.
$this -> data [ 'Invoice' ][ 'due_date' ] = date ( " Y-m-d " , strtotime ( " +30 days " ));
2011-08-14 23:04:38 -07:00
2011-08-11 22:46:26 -07:00
2013-04-13 03:01:47 -07:00
$this -> data [ 'Invoice' ][ 'currency_id' ] = 2 ; //All new Invoices default to AUD.
2013-06-07 18:40:38 -07:00
2013-05-29 14:49:44 -07:00
if ( isset ( $source_document_id )) {
//This is not ideal. But nothing else is either.
$sourceDoc = $this -> Document -> find ( 'first' , array ( 'conditions' => array ( 'Document.id' => $source_document_id )));
if ( $sourceDoc [ 'Document' ][ 'type' ] == 'orderAck' ) {
2022-07-06 22:32:20 -07:00
$oa_attributes = array (
2013-05-29 14:49:44 -07:00
'ship_via' ,
'fob' ,
'estimated_delivery'
);
2022-07-06 22:32:20 -07:00
foreach ( $oa_attributes as $attr ) {
2013-05-29 14:49:44 -07:00
$this -> data [ 'Invoice' ][ $attr ] = $sourceDoc [ 'OrderAcknowledgement' ][ $attr ];
}
}
}
2011-10-23 20:20:07 -07:00
2013-04-13 03:01:47 -07:00
break ;
case " purchaseOrder " :
2013-06-23 01:31:21 -07:00
//$enquiry = $this->Document->OrderAcknowledgement->Enquiry->read(null, $enquiryid);
2013-04-13 03:01:47 -07:00
$count = $this -> Document -> PurchaseOrder -> findCount ();
$count ++ ;
$offset = 2060 ;
$count += $offset ;
$newPOnumber = " CMCPO " . $count ;
$this -> data [ 'PurchaseOrder' ][ 'title' ] = $newPOnumber ;
$this -> data [ 'PurchaseOrder' ][ 'issue_date' ] = date ( 'Y-m-d' );
2013-06-15 01:32:14 -07:00
$this -> data [ 'PurchaseOrder' ][ 'deliver_to' ] = <<< ENDDELIVER
CMC TECHNOLOGIES PTY LTD < br >
Unit 19 , 77 Bourke Road < br >
Alexandria NSW 2015 AUSTRALIA < br >
ENDDELIVER ;
//Fuck it all.
$this -> data [ 'PurchaseOrder' ][ 'shipping_instructions' ] = <<< ENDINSTRUCTIONS
< b > PART SHIPMENT :</ b > Not Acceptable - please advise ASAP if production of an Item will Delay the Dispatch of the Complete Order by the Date Required stated above .< br >
< b > INSURANCE :</ b > DO NOT INSURE - Insurance effected by CMC < br >
< b > SECURITY :</ b > It is essential that the cargo is prepared & handled so as not to compromise its security standing .< br >
< b > SHIPMENT MARKINGS :</ b >< br >
( Please put red fragile stickers on boxing ) < br >
CMC TECHNOLOGIES PTY LTD < br >
UNIT 19 , 77 BOURKE ROAD < br >
ALEXANDRIA NSW 2015 AUSTRALIA < br >
ENDINSTRUCTIONS ;
2019-07-03 22:58:46 -07:00
$currentuser = $this -> GetCurrentUser ();
$this -> data [ 'Document' ][ 'user_id' ] = $currentuser [ 'User' ][ 'id' ];
2013-04-13 03:01:47 -07:00
$this -> data [ 'Document' ][ 'revision' ] = 0 ; //No revisions for POs. I have decreed it!
2019-12-17 20:06:53 -08:00
$this -> data [ 'Document' ][ 'doc_page_count' ] = 0 ;
2013-04-13 03:01:47 -07:00
$this -> data [ 'Document' ][ 'type' ] = 'purchaseOrder' ;
break ;
2013-06-15 01:32:14 -07:00
2013-04-13 03:01:47 -07:00
case " orderAck " :
$enquiry = $this -> Document -> OrderAcknowledgement -> Enquiry -> read ( null , $enquiryid );
$this -> data [ 'OrderAcknowledgement' ][ 'job_id' ] = $jobid ;
$this -> data [ 'OrderAcknowledgement' ][ 'enquiry_id' ] = $enquiryid ;
$this -> data [ 'OrderAcknowledgement' ][ 'currency_id' ] = 2 ; //Default to AUD.
$this -> data [ 'Document' ][ 'user_id' ] = $enquiry [ 'Enquiry' ][ 'user_id' ];
$this -> data [ 'Document' ][ 'revision' ] = 0 ; //No revisions for Order Acks. I have decreed it!
$this -> data [ 'Document' ][ 'type' ] = 'orderAck' ;
break ;
2011-08-11 00:30:03 -07:00
2013-06-08 23:37:00 -07:00
case " packingList " ;
2013-06-10 02:27:23 -07:00
$this -> data [ 'Document' ][ 'type' ] = 'packingList' ;
$packing_number_offset = 6500 ; //What PL number we are up to. Starting at 6500 to avoid conflicts
$number_of_packinglists = $this -> Document -> PackingList -> findCount ();
$enquiry = $this -> Document -> PackingList -> Enquiry -> read ( null , $enquiryid );
if ( ! $enquiry ) {
$this -> Session -> setFlash ( 'Invalid Enquiry ID' );
$this -> redirect ( array ( 'action' => 'index' ));
return ;
}
2010-07-07 23:18:04 -07:00
2013-06-10 02:27:23 -07:00
$newPackingNumber = $packing_number_offset + $number_of_packinglists ;
$this -> data [ 'PackingList' ][ 'enquiry_id' ] = $enquiryid ;
$this -> data [ 'PackingList' ][ 'title' ] = " CMCPL " . $newPackingNumber ;
$this -> data [ 'PackingList' ][ 'job_id' ] = $jobid ;
$this -> data [ 'PackingList' ][ 'customer_id' ] = $enquiry [ 'Enquiry' ][ 'customer_id' ];
2011-08-08 23:33:21 -07:00
2013-06-10 02:27:23 -07:00
$this -> data [ 'Document' ][ 'user_id' ] = $enquiry [ 'Enquiry' ][ 'user_id' ];
//$number_of_revisions = $this->Document->Invoice->findCount('Invoice.enquiry_id ='. $enquiryid);
$this -> data [ 'Document' ][ 'revision' ] = 0 ; //No revisions for Invoices. I have decreed it!
$this -> data [ 'Document' ][ 'type' ] = 'packingList' ;
2011-09-17 02:33:17 -07:00
2013-06-10 02:27:23 -07:00
$this -> data [ 'PackingList' ][ 'issue_date' ] = date ( 'Y-m-d' ); //Argh. Quote model uses 'date_issed'.
2010-07-07 23:18:04 -07:00
2011-10-23 20:20:07 -07:00
2010-07-07 23:18:04 -07:00
2012-11-16 19:36:36 -08:00
2013-06-10 02:27:23 -07:00
if ( isset ( $source_document_id )) {
//This is not ideal. But nothing else is either.
$sourceDoc = $this -> Document -> find ( 'first' , array ( 'conditions' => array ( 'Document.id' => $source_document_id )));
2022-07-06 22:32:20 -07:00
2013-06-10 02:27:23 -07:00
if ( $sourceDoc [ 'Document' ][ 'type' ] == 'orderAck' ) {
2022-07-06 22:32:20 -07:00
foreach ( $source_attributes as $attr ) {
2013-06-10 02:27:23 -07:00
$this -> data [ 'PackingList' ][ $attr ] = $sourceDoc [ 'OrderAcknowledgement' ][ $attr ];
}
}
if ( $sourceDoc [ 'Document' ][ 'type' ] == 'invoice' ) {
2022-07-06 22:32:20 -07:00
$source_attributes = array (
'ship_via' ,
'fob'
);
foreach ( $source_attributes as $attr ) {
2013-06-10 02:27:23 -07:00
$this -> data [ 'PackingList' ][ $attr ] = $sourceDoc [ 'Invoice' ][ $attr ];
}
}
2012-11-16 19:36:36 -08:00
2013-06-10 02:27:23 -07:00
}
2012-11-16 19:36:36 -08:00
2013-06-08 23:37:00 -07:00
break ;
2013-04-13 03:01:47 -07:00
}
2012-11-16 19:36:36 -08:00
2013-04-22 05:57:57 -07:00
if ( isset ( $source_document_id )) {
$sourceDoc = $this -> Document -> find ( 'first' , array ( 'conditions' => array ( 'Document.id' => $source_document_id )));
2011-08-09 01:54:10 -07:00
2013-05-29 14:49:44 -07:00
$copy_doc_attributes = array ( 'cmc_reference' , 'shipping_details' , 'bill_to' , 'ship_to' );
foreach ( $copy_doc_attributes as $attr ) {
$this -> data [ 'Document' ][ $attr ] = $sourceDoc [ 'Document' ][ $attr ];
}
2011-08-11 22:46:26 -07:00
2013-06-08 21:09:30 -07:00
$this -> data [ 'LineItem' ] = $this -> copy_related ( $sourceDoc , 'LineItem' , array ( 'id' , 'document_id' , 'costing_id' ));
2011-08-11 22:46:26 -07:00
2013-04-22 05:57:57 -07:00
}
2013-04-13 03:01:47 -07:00
if ( $this -> Document -> saveAll ( $this -> data )) {
$newid = $this -> Document -> id ;
$this -> Session -> setFlash ( __ ( 'The Document has been saved' , true ));
$this -> redirect ( array ( 'action' => 'view' , $newid ));
} else {
$this -> Session -> setFlash ( __ ( 'The Document could not be saved. Something went wrong! Ensure all fields are entered correctly.' , true ));
}
2011-08-11 22:46:26 -07:00
2011-05-19 00:05:01 -07:00
2013-04-13 03:01:47 -07:00
}
2011-05-19 00:05:01 -07:00
2013-04-13 03:01:47 -07:00
/**
* Revise a Document .
*
* @ param int $id - the document ID we want to make a new revision of .
* @ return < type >
*/
2012-11-16 19:36:36 -08:00
2013-04-13 03:01:47 -07:00
function revise ( $id = null ) {
if ( ! $id ) {
$this -> Session -> setFlash ( 'Invalid Document ID' );
return ;
}
2012-11-16 19:36:36 -08:00
2013-04-13 03:01:47 -07:00
$document = $this -> Document -> find ( 'first' , array ( 'conditions' => array ( 'Document.id' => $id )));
$this -> set ( 'document' , $document );
2012-11-18 12:19:55 -08:00
2013-04-13 03:01:47 -07:00
$this -> Document -> create ();
2012-11-18 12:19:55 -08:00
2013-04-13 03:01:47 -07:00
if ( ! empty ( $document [ 'Invoice' ][ 'id' ])) {
echo " WE HAVE AN INVOICE " ;
}
else if ( ! empty ( $document [ 'Quote' ][ 'id' ])) {
2010-07-07 23:18:04 -07:00
2013-04-13 03:01:47 -07:00
//First setup the Date and default valid until date. (30 days from today).
2010-07-07 23:18:04 -07:00
2013-04-13 03:01:47 -07:00
$today_date = date ( " Y-m-d " );
$valid_until = date ( " Y-m-d " , strtotime ( " +30 days " ));
2011-08-11 00:30:03 -07:00
2013-04-13 03:01:47 -07:00
$newDoc = array ();
2011-08-11 22:46:26 -07:00
2013-04-13 03:01:47 -07:00
$newDoc [ 'Quote' ] = $this -> unset_keys ( $document [ 'Quote' ], array ( 'id' , 'created' ));
2011-08-11 22:46:26 -07:00
2013-04-13 03:01:47 -07:00
$newDoc [ 'Quote' ][ 'date_issued' ] = $today_date ;
$newDoc [ 'Quote' ][ 'days_valid' ] = 30 ;
$newDoc [ 'Quote' ][ 'valid_until' ] = $valid_until ;
2011-08-11 22:46:26 -07:00
2013-04-13 03:01:47 -07:00
$enquiryid = $document [ 'Quote' ][ 'enquiry_id' ];
$number_of_revisions = $this -> Document -> Quote -> findCount ( 'Quote.enquiry_id =' . $enquiryid );
$newDoc [ 'Document' ][ 'revision' ] = $number_of_revisions ;
$newDoc [ 'Document' ][ 'type' ] = 'quote' ;
2011-08-11 22:46:26 -07:00
2013-04-13 03:01:47 -07:00
$newDoc [ 'DocPage' ] = $document [ 'DocPage' ];
2011-08-11 22:46:26 -07:00
2013-04-13 03:01:47 -07:00
foreach ( $newDoc [ 'DocPage' ] as $index => $li ) {
$newDoc [ 'DocPage' ][ $index ][ 'id' ] = null ;
$newDoc [ 'DocPage' ][ $index ][ 'document_id' ] = null ;
}
2011-08-11 22:46:26 -07:00
2013-04-13 03:01:47 -07:00
}
2011-08-14 23:04:38 -07:00
2013-04-13 03:01:47 -07:00
else if ( ! empty ( $document [ 'PurchaseOrder' ][ 'id' ])) {
echo " WE ARE REVISING A PO " ;
}
2011-08-11 22:46:26 -07:00
2011-10-23 20:20:07 -07:00
2013-04-13 03:01:47 -07:00
else if ( ! empty ( $document [ 'OrderAcknowledgement' ][ 'id' ])) {
echo " WE ARE REVISING An ORDER ACK " ;
}
2011-10-23 20:20:07 -07:00
2011-08-11 00:30:03 -07:00
2013-04-13 03:01:47 -07:00
/** Copy the LineItems from this Document. Applies to all of the Document Types */
2013-06-08 21:09:30 -07:00
$newDoc [ 'LineItem' ] = $this -> copy_related ( $document , 'LineItem' , array ( 'id' , 'document_id' , 'costing_id' ));
2011-08-11 00:30:03 -07:00
2013-06-08 21:09:30 -07:00
$newDoc [ 'DocumentAttachment' ] = $this -> copy_related ( $document , 'DocumentAttachment' , 'id' , 'document_id' );
2011-08-11 00:30:03 -07:00
2013-04-13 03:01:47 -07:00
$this -> set ( 'newDoc' , $newDoc );
2013-06-08 21:09:30 -07:00
2013-04-13 03:01:47 -07:00
if ( $this -> Document -> saveAll ( $newDoc )) {
$newid = $this -> Document -> id ;
$this -> Session -> setFlash ( __ ( " Revision { $number_of_revisions } created " , true ));
$this -> redirect ( array ( 'action' => 'view' , $newid ));
} else {
$this -> Session -> setFlash ( __ ( 'The Document could not be saved. Please, try again.' , true ));
}
2011-08-11 00:30:03 -07:00
2013-04-13 03:01:47 -07:00
}
2011-08-11 00:30:03 -07:00
2013-04-13 03:01:47 -07:00
function ajax_edit ( $id = null ) {
$this -> layout = 'ajax' ;
2011-09-01 00:42:32 -07:00
2013-04-13 03:01:47 -07:00
if ( ! $id && empty ( $this -> data )) {
echo 'Invalid Document ID' ;
}
if ( ! empty ( $this -> data )) {
$docID = $this -> data [ 'Document' ][ 'id' ];
2019-12-17 20:06:53 -08:00
//this is retarded. I think we delete and re-create each page each time..
2013-04-13 03:01:47 -07:00
$this -> Document -> DocPage -> deleteAll ( array ( 'document_id' => $docID ));
$this -> Document -> DocPage -> updateCounterCache ( array ( 'document_id' => $docID ));
2011-08-11 22:46:26 -07:00
2013-06-23 01:31:21 -07:00
if ( isset ( $this -> data [ 'PurchaseOrder' ])) {
//Delete all the existing JobPurchaseOrder relationships for this PO.
//Fuck it. @TODO SQL injection potential here.
$query = " DELETE FROM jobs_purchase_orders WHERE purchase_order_id = " . $this -> data [ 'PurchaseOrder' ][ 'id' ];
$result = $this -> Document -> query ( $query );
2011-08-11 22:46:26 -07:00
2013-06-23 01:31:21 -07:00
}
2011-08-11 22:46:26 -07:00
2013-06-23 01:31:21 -07:00
if ( isset ( $this -> data [ 'PurchaseOrder' ][ 'Job' ])) {
2019-07-10 23:55:11 -07:00
//Loop over the submitted Jobs and add them. Fuck CakePHP is a piece of shit.
2013-06-23 01:31:21 -07:00
//This is easier than using the fucking built in crap that doesnt work.
//Fuck everything about this.
$po_id = $this -> data [ 'PurchaseOrder' ][ 'id' ];
foreach ( $this -> data [ 'PurchaseOrder' ][ 'Job' ] as $job_id ) {
2019-07-09 00:12:26 -07:00
$query = " INSERT INTO `cmc`.`jobs_purchase_orders` (`id`, `job_id`, `purchase_order_id`) VALUES (NULL, ' { $job_id } ', ' { $po_id } '); " ;
2013-06-23 01:31:21 -07:00
$this -> Document -> query ( $query );
}
$result = $this -> Document -> PurchaseOrder -> saveAll ( $this -> data [ 'PurchaseOrder' ]);
2011-08-14 23:04:38 -07:00
2013-06-23 01:31:21 -07:00
//die(print_r($this->data['PurchaseOrder']));
//die(print_r($result));
//die(print_r($this->data));
}
2011-08-11 22:46:26 -07:00
2013-04-13 03:01:47 -07:00
if ( $this -> Document -> saveAll ( $this -> data )) {
2019-12-17 20:06:53 -08:00
echo 'SUCCESS' ; //this is so stupid
2013-04-13 03:01:47 -07:00
} else {
echo 'FAILURE' ;
}
}
else {
echo 'FAILURE' ;
}
}
2011-08-11 22:46:26 -07:00
2013-04-13 03:01:47 -07:00
/**
*
* Convert a Quote to an OrderAcknowledgement .
*
* Create a new Job for the Enquiry .
*
* Make a new Document ( orderack ) with the same LineItems as Quote with Document ID #$id
*
*
*/
function convert_to_oa () {
$this -> layout = 'ajax' ;
if ( empty ( $this -> data )) {
echo 'FAILURE' ;
}
else {
$id = $this -> data [ 'Document' ][ 'id' ];
$originalDoc = $this -> Document -> find ( 'first' , array ( 'conditions' => array ( 'Document.id' => $id )));
$enquiry = $this -> Document -> OrderAcknowledgement -> Enquiry -> find ( 'first' , array ( 'conditions' => array ( 'Enquiry.id' => $originalDoc [ 'Quote' ][ 'enquiry_id' ])));
$enquiry [ 'Enquiry' ][ 'status_id' ] = 3 ; //Job Won, Order Placed
$this -> Document -> OrderAcknowledgement -> Enquiry -> save ( $enquiry );
$customer = $this -> Document -> OrderAcknowledgement -> Enquiry -> Customer -> find ( 'first' , array ( 'conditions' => array ( 'Customer.id' => $enquiry [ 'Enquiry' ][ 'customer_id' ])));
$customer [ 'Customer' ][ 'customer_category_id' ] = 3 ; //make Customer a Creditor
$this -> Document -> OrderAcknowledgement -> Enquiry -> Customer -> save ( $customer );
$newDoc = array ();
if ( $this -> data [ 'Job' ][ 'create_new_job' ]) {
$job = $this -> Document -> OrderAcknowledgement -> Job -> newJob ( $enquiry , $this -> data [ 'Job' ][ 'customer_order_number' ]); //Only way to know Enquiry ID is if it's a Quote.
$newDoc [ 'OrderAcknowledgement' ][ 'job_id' ] = $job [ 'Job' ][ 'id' ];
}
$newDoc [ 'Document' ][ 'type' ] = 'orderAck' ;
$newDoc [ 'OrderAcknowledgement' ][ 'enquiry_id' ] = $enquiry [ 'Enquiry' ][ 'id' ];
$newDoc [ 'OrderAcknowledgement' ][ 'currency_id' ] = 2 ; //Default to AUD.
$newDoc [ 'OrderAcknowledgement' ][ 'issue_date' ] = date ( 'Y-m-d' );
$newDoc [ 'Document' ][ 'user_id' ] = $enquiry [ 'Enquiry' ][ 'user_id' ];
$newDoc [ 'Document' ][ 'revision' ] = 0 ; //No revisions for Order Acks. I have decreed it!
$newDoc [ 'Document' ][ 'type' ] = 'orderAck' ;
if ( isset ( $enquiry [ 'BillingAddress' ][ 'id' ])) {
$newDoc [ 'Document' ][ 'bill_to' ] = '<b>' . $customer [ 'Customer' ][ 'name' ] . '</b><br/>' ;
$newDoc [ 'Document' ][ 'bill_to' ] .= $this -> Document -> OrderAcknowledgement -> Enquiry -> formatAddress ( $enquiry [ 'BillingAddress' ][ 'id' ], 'BillingAddress' );
}
if ( isset ( $enquiry [ 'ShippingAddress' ][ 'id' ])) {
$newDoc [ 'Document' ][ 'ship_to' ] = '<b>' . $customer [ 'Customer' ][ 'name' ] . '</b><br/>' ;
$newDoc [ 'Document' ][ 'ship_to' ] .= $this -> Document -> OrderAcknowledgement -> Enquiry -> formatAddress ( $enquiry [ 'ShippingAddress' ][ 'id' ], 'ShippingAddress' );
}
2013-06-08 21:09:30 -07:00
$newDoc [ 'LineItem' ] = $this -> copy_related ( $originalDoc , 'LineItem' , array ( 'id' , 'document_id' , 'costing_id' ));
$newDoc [ 'DocumentAttachment' ] = $this -> copy_related ( $originalDoc , 'DocumentAttachment' , array ( 'id' , 'document_id' ));
2013-04-13 03:01:47 -07:00
/** Copy the LineItems from this Document. Applies to all of the Document Types */
2013-06-08 21:09:30 -07:00
2013-04-13 03:01:47 -07:00
if ( $this -> Document -> saveAll ( $newDoc )) {
$newid = $this -> Document -> id ;
2013-05-29 14:49:44 -07:00
if ( isset ( $job )) {
2013-04-13 03:01:47 -07:00
$this -> Session -> setFlash ( __ ( " Job { $job [ 'Job' ][ 'title' ] } and Order Acknowledgement created " , true ));
}
else {
$this -> Session -> setFlash ( __ ( " Order Acknowledgement created " , true ));
}
$this -> redirect ( array ( 'action' => 'view' , $newid ));
} else {
$this -> Session -> setFlash ( __ ( 'The Document could not be saved. Please, try again.' , true ));
}
$this -> Document -> saveAll ( $newDoc );
}
}
2011-08-11 00:30:03 -07:00
2013-04-13 03:01:47 -07:00
function pdf ( $id = null ) {
//Configure::write('debug',0);
if ( ! $id ) {
$this -> Session -> setFlash ( __ ( 'Invalid Document' , true ));
$this -> redirect ( array ( 'controller' => 'documents' , 'action' => 'index' ));
}
2011-09-09 02:23:54 -07:00
2013-04-13 03:01:47 -07:00
$document = $this -> Document -> read ( null , $id );
2011-09-01 00:42:32 -07:00
2013-04-13 03:01:47 -07:00
$this -> set ( 'document' , $document );
2011-09-01 00:42:32 -07:00
2013-04-13 03:01:47 -07:00
$this -> data = $document ;
2011-09-01 00:42:32 -07:00
2013-04-13 03:01:47 -07:00
$docType = $this -> Document -> getDocType ( $document );
$docTypeFullName = $this -> Document -> getDocFullName ( $docType );
2011-09-01 00:42:32 -07:00
2011-05-24 02:11:07 -07:00
2011-09-04 18:43:21 -07:00
2013-04-13 03:01:47 -07:00
$currency = $this -> Document -> getCurrency ( $document );
$enquiry = $this -> Document -> getEnquiry ( $document );
2011-09-04 18:43:21 -07:00
2013-04-13 03:01:47 -07:00
$this -> set ( 'docType' , $docType );
$this -> set ( 'docTypeFullName' , strtoupper ( $docTypeFullName ));
$this -> set ( 'currency' , $currency );
$this -> set ( 'currencyCode' , $currency [ 'Currency' ][ 'iso4217' ]);
$this -> set ( 'currencySymbol' , $currency [ 'Currency' ][ 'symbol' ]);
2011-09-08 18:28:29 -07:00
2013-04-13 03:01:47 -07:00
$gst = $enquiry [ 'Enquiry' ][ 'gst' ];
2011-09-04 18:43:21 -07:00
2013-04-13 03:01:47 -07:00
$this -> set ( 'gst' , $gst );
2012-11-16 19:36:36 -08:00
2013-04-13 03:01:47 -07:00
if ( $gst == 1 ) {
$totalsDescText = array (
'subtotal' => 'SUBTOTAL' ,
'gst' => 'GST (10%)' ,
'total' => 'TOTAL DUE'
);
}
else {
$totalsDescText = array (
'subtotal' => 'SUBTOTAL' ,
'gst' => 'GST (10%)' ,
'total' => 'TOTAL PAYABLE'
);
}
2012-11-16 19:36:36 -08:00
2013-04-13 03:01:47 -07:00
$totals = $this -> calculateTotals ( $document , $gst );
$this -> set ( 'totals' , $totals );
$this -> set ( 'totalsDescText' , $totalsDescText );
2012-11-17 18:57:16 -08:00
2013-04-13 03:01:47 -07:00
$this -> set ( 'currency' , $currency );
$this -> set ( 'enquiry' , $enquiry );
2012-11-16 19:36:36 -08:00
2013-04-13 03:01:47 -07:00
$this -> set ( 'principlesList' , $this -> Document -> LineItem -> Product -> Principle -> find ( 'list' ));
2012-11-16 19:36:36 -08:00
2013-04-13 03:01:47 -07:00
$this -> set ( 'products' , $document [ 'LineItem' ]);
$this -> set ( 'states' , $this -> Document -> Invoice -> Enquiry -> State -> find ( 'list' ));
2012-11-16 19:36:36 -08:00
2013-04-13 03:01:47 -07:00
//Set filename for the document.
//
switch ( $docType ) {
case " quote " :
$filename = $enquiry [ 'Enquiry' ][ 'title' ];
$template_name = 'pdf_quote' ;
break ;
2012-11-16 19:36:36 -08:00
2013-04-13 03:01:47 -07:00
case " invoice " :
$filename = $document [ 'Invoice' ][ 'title' ];
$this -> set ( 'docTitle' , $document [ 'Invoice' ][ 'title' ]);
$this -> set ( 'job' , $this -> Document -> Invoice -> Job -> find ( 'first' , array ( 'conditions' => array ( 'Job.id' => $document [ 'Invoice' ][ 'job_id' ]))));
$this -> set ( 'issue_date_string' , date ( 'd F Y' , strtotime ( $document [ 'Invoice' ][ 'issue_date' ])));
2012-11-17 18:57:16 -08:00
2013-04-13 03:01:47 -07:00
$this -> set ( 'ship_via' , $document [ 'Invoice' ][ 'ship_via' ]);
$this -> set ( 'fob' , $document [ 'Invoice' ][ 'fob' ]);
$template_name = 'pdf_invoice' ;
break ;
2011-05-24 02:11:07 -07:00
2013-04-13 03:01:47 -07:00
case " purchaseOrder " :
2013-06-26 07:03:24 -07:00
$principle = $this -> Document -> LineItem -> Product -> Principle -> find ( 'first' , array ( 'conditions' => array ( 'Principle.id' => $document [ 'PurchaseOrder' ][ 'principle_id' ])));
2013-06-15 01:32:14 -07:00
2013-06-26 07:03:24 -07:00
$this -> set ( 'principle' , $principle );
$purchase_order = $this -> Document -> PurchaseOrder -> find ( 'first' ,
array ( 'conditions' => array ( 'PurchaseOrder.id' => $document [ 'PurchaseOrder' ][ 'id' ])));
$this -> set ( 'purchase_order' , $purchase_order );
2013-06-15 01:32:14 -07:00
$totalsDescText = array (
'subtotal' => 'SUBTOTAL' ,
2013-06-26 07:03:24 -07:00
'gst' => 'GST' ,
2013-06-15 01:32:14 -07:00
'total' => 'ORDER TOTAL'
);
//No GST for POs for now.
2013-06-26 07:03:24 -07:00
if ( $principle [ 'Country' ][ 'id' ] == 1 ) {
$totals = $this -> calculateTotals ( $document , 1 );
}
else {
$totalsDescText [ 'gst' ] = '' ;
$totals = $this -> calculateTotals ( $document , 0 );
}
2013-06-15 01:32:14 -07:00
$this -> set ( 'totals' , $totals );
$this -> set ( 'totalsDescText' , $totalsDescText );
2013-04-13 03:01:47 -07:00
$filename = $document [ 'PurchaseOrder' ][ 'title' ];
2013-06-15 01:32:14 -07:00
$issue_date = date ( 'l, j F Y' , strtotime ( $document [ 'PurchaseOrder' ][ 'issue_date' ]));
$this -> set ( 'issue_date' , $issue_date );
2013-04-13 20:14:11 -07:00
$template_name = 'pdf_po' ;
2013-04-13 03:01:47 -07:00
break ;
2011-05-24 02:11:07 -07:00
2013-04-13 03:01:47 -07:00
case " orderAck " :
2011-05-24 02:11:07 -07:00
2013-04-13 03:01:47 -07:00
$job = $this -> Document -> OrderAcknowledgement -> Job -> find ( 'first' , array ( 'conditions' => array ( 'Job.id' => $document [ 'OrderAcknowledgement' ][ 'job_id' ])));
2011-08-08 23:33:21 -07:00
2013-04-13 03:01:47 -07:00
$filename = $job [ 'Job' ][ 'title' ] . '_OrderAcknowledgement' ;
$this -> set ( 'docTitle' , $job [ 'Job' ][ 'title' ]);
2011-08-08 23:33:21 -07:00
2013-04-13 03:01:47 -07:00
$this -> set ( 'docTypeFullName' , 'JOB' ); //So it doesnt say CMC order Ack # in the pdf_shipping_billing_box. This string is too long
$this -> set ( 'job' , $job );
$this -> set ( 'issue_date_string' , date ( 'd F Y' , strtotime ( $document [ 'OrderAcknowledgement' ][ 'issue_date' ])));
2011-05-24 02:11:07 -07:00
2013-04-13 03:01:47 -07:00
$this -> set ( 'ship_via' , $document [ 'OrderAcknowledgement' ][ 'ship_via' ]);
$this -> set ( 'fob' , $document [ 'OrderAcknowledgement' ][ 'fob' ]);
$this -> set ( 'estimated_delivery' , $document [ 'OrderAcknowledgement' ][ 'estimated_delivery' ]);
2011-05-24 02:11:07 -07:00
2013-04-13 03:01:47 -07:00
$template_name = 'pdf_orderack' ;
break ;
2011-05-24 02:11:07 -07:00
2013-06-10 02:27:23 -07:00
case " packingList " :
$filename = $document [ 'PackingList' ][ 'title' ];
$this -> set ( 'docTitle' , $document [ 'PackingList' ][ 'title' ]);
$this -> set ( 'job' , $this -> Document -> PackingList -> Job -> find ( 'first' , array ( 'conditions' => array ( 'Job.id' => $document [ 'PackingList' ][ 'job_id' ]))));
$this -> set ( 'issue_date_string' , date ( 'd F Y' , strtotime ( $document [ 'PackingList' ][ 'issue_date' ])));
$this -> set ( 'ship_via' , $document [ 'PackingList' ][ 'ship_via' ]);
$this -> set ( 'fob' , $document [ 'PackingList' ][ 'fob' ]);
$template_name = 'pdf_packinglist' ;
break ;
2013-04-13 03:01:47 -07:00
}
2011-05-24 02:11:07 -07:00
2013-04-13 03:01:47 -07:00
if ( $document [ 'Document' ][ 'revision' ] > 0 ) {
$filename = $filename . '_' . $document [ 'Document' ][ 'revision' ] . '.pdf' ;
}
else {
$filename = $filename . '.pdf' ;
}
2011-05-24 02:11:07 -07:00
2013-04-13 03:01:47 -07:00
$this -> set ( 'filename' , $filename );
$document [ 'Document' ][ 'pdf_filename' ] = $filename ;
$document [ 'Document' ][ 'pdf_created_at' ] = date ( 'Y-m-d H:i:s' );
$document [ 'Document' ][ 'pdf_created_by_user_id' ] = $this -> getCurrentUserID ();
if ( $this -> Document -> save ( $document )) {
//echo "Set pdf_filename attritbute to: ".$filename;
}
else {
//echo 'Failed to set pdf_filename to: '.$filename;
}
2011-05-24 02:11:07 -07:00
2013-04-13 03:01:47 -07:00
$colWidths = array (
'item' => '7%' ,
'qty' => '7%' ,
'desc' => '56%' ,
'unit' => '15%' ,
'total' => '15%'
2011-05-24 02:11:07 -07:00
2013-04-13 03:01:47 -07:00
);
2011-05-24 02:11:07 -07:00
2013-04-13 03:01:47 -07:00
$this -> set ( 'colWidths' , $colWidths );
2011-05-24 02:11:07 -07:00
2013-04-13 03:01:47 -07:00
$this -> render ( $template_name );
}
2011-05-24 02:11:07 -07:00
2013-04-13 03:01:47 -07:00
//Called via AJAX to generate Quotation First Pages.
function generateFirstPage ( $id = null ) {
$this -> layout = 'ajax' ;
2011-05-24 02:11:07 -07:00
2013-04-13 03:01:47 -07:00
if ( ! $id ) {
return ;
}
2011-08-14 23:04:38 -07:00
2013-04-13 03:01:47 -07:00
$document = $this -> Document -> read ( null , $id );
$this -> set ( 'document' , $document );
$this -> set ( 'attachments' , $this -> Document -> DocumentAttachment -> find ( 'all' , array ( 'conditions' => array ( 'document_id' => $id ))));
$enquiry = $this -> Document -> Quote -> Enquiry -> read ( null , $document [ 'Quote' ][ 'enquiry_id' ]);
$this -> set ( 'enquiry' , $enquiry );
}
2022-07-06 22:32:20 -07:00
2016-11-04 20:10:55 -07:00
function email_format ( $a ) {
return " < " . $a . " > " ;
}
2021-09-18 22:07:16 -07:00
// Do a bunch of queries to build a subject line for PO emails.
function po_email_subject ( $document ) {
$id = $document [ 'id' ];
$subject = 'CMC Technologies Purchase Order: ' . $document [ 'title' ];
$q = <<< EOT
2022-07-06 22:32:20 -07:00
SELECT jobs . id , title FROM jobs
2021-09-18 22:07:16 -07:00
JOIN jobs_purchase_orders as jpo ON ( jpo . purchase_order_id = $id AND jpo . job_id = jobs . id )
EOT ;
$jobs = $this -> Document -> query ( $q );
if ( count ( $jobs ) <= 0 ) {
return $subject ;
}
$job_numbers = array ();
$job_ids = array ();
foreach ( $jobs as $job ) {
$job_numbers [] = $job [ " jobs " ][ " title " ];
$job_ids [] = $job [ " jobs " ][ " id " ];
}
$job_ids_in = implode ( " , " , $job_ids );
$q = <<< EOT
SELECT enquiries . title from enquiries join jobs on enquiries . id = jobs . enquiry_id AND jobs . id IN ( $job_ids_in );
EOT ;
$enquiries = $this -> Document -> query ( $q );
$enquiry_numbers = array ();
foreach ( $enquiries as $enquiry ) {
$enquiry_numbers [] = $enquiry [ " enquiries " ][ " title " ];
}
$subject = $subject . ' ( ' . implode ( " " , $job_numbers ) . ' )' ;
$subject = $subject . ' ( ' . implode ( " " , $enquiry_numbers ) . ' )' ;
return $subject ;
}
2022-06-25 00:06:48 -07:00
function orderack_email_subject ( $document ) {
$id = $document [ 'Document' ][ 'id' ];
//print_r($document);
$q = <<< EOT
SELECT j . title as job_title , e . title as enquiry_title
FROM order_acknowledgements oa
JOIN enquiries e ON oa . enquiry_id = e . id
JOIN jobs j on oa . job_id = j . id
WHERE oa . document_id = $id ;
EOT ;
$titles = $this -> Document -> query ( $q );
2022-06-25 17:00:05 -07:00
if ( count ( $titles ) < 1 ) {
2022-06-25 00:06:48 -07:00
// This probably shouldn't happen, but who knows.
$subject = 'CMC Technologies Order Acknowledgement' ;
return $subject ;
}
2022-07-06 22:32:20 -07:00
$subject = 'Order Acknowledgement: ' . $titles [ 0 ][ 'e' ][ 'enquiry_title' ] . ' ' . $titles [ 0 ][ 'j' ][ 'job_title' ];
2022-06-25 00:06:48 -07:00
return $subject ;
}
2022-06-25 17:02:28 -07:00
2022-07-06 22:32:20 -07:00
function invoice_email_subject ( $document ) {
$id = $document [ 'Document' ][ 'id' ];
//print_r($document);
$q = <<< EOT
SELECT i . title as invoice_title , j . title as job_title , e . title as enquiry_title
FROM invoices i
JOIN enquiries e ON i . enquiry_id = e . id
JOIN jobs j on i . job_id = j . id
WHERE i . document_id = $id ;
EOT ;
$titles = $this -> Document -> query ( $q );
if ( count ( $titles ) < 1 ) {
// This probably shouldn't happen, but who knows.
$subject = 'Invoice' ;
return $subject ;
}
$subject = 'Invoice: ' . $titles [ 0 ][ 'i' ][ 'invoice_title' ] . ' ' . $titles [ 0 ][ 'e' ][ 'enquiry_title' ] . ' ' . $titles [ 0 ][ 'j' ][ 'job_title' ];
return $subject ;
}
2012-11-16 19:36:36 -08:00
/**
* Email the PDF ( document + attachments ) for this Document to a Recipient ( Customer or Principle ) .
*
*
* @ param < int > $id - Document ID
*/
function email_pdf ( $id ) {
//Setup the SMTP Options
$this -> Email -> smtpOptions = Configure :: read ( 'smtp_settings' );
$this -> Email -> delivery = 'smtp' ;
2022-07-06 22:32:20 -07:00
$document = $this -> Document -> read ( null , $id );
2012-11-18 12:19:55 -08:00
2012-11-16 19:36:36 -08:00
if ( empty ( $document [ 'Document' ][ 'pdf_filename' ])) {
$this -> Session -> setFlash ( __ ( 'Error. Please generate the PDF before attempting to email it' , true ));
return ;
}
else {
$pdf_dir = Configure :: read ( 'pdf_directory' );
2012-11-18 12:19:55 -08:00
$attachment_files = array ( $pdf_dir . $document [ 'Document' ][ 'pdf_filename' ]);
2012-12-01 18:17:09 -08:00
foreach ( $document [ 'DocumentAttachment' ] as $document_attachment ) {
$attachment = $this -> Document -> DocumentAttachment -> Attachment -> read ( null , $document_attachment [ 'attachment_id' ]);
$attachment_files [] = $attachment [ 'Attachment' ][ 'file' ];
2012-11-18 12:19:55 -08:00
}
$this -> Email -> attachments = $attachment_files ;
2022-07-06 22:32:20 -07:00
2012-11-16 19:36:36 -08:00
}
$enquiry = $this -> Document -> getEnquiry ( $document );
2016-11-04 20:10:55 -07:00
$this -> Email -> to = $this -> email_format ( $enquiry [ 'Contact' ][ 'email' ]);
2022-07-06 22:32:20 -07:00
2012-11-16 19:36:36 -08:00
2016-11-04 20:10:55 -07:00
$this -> Email -> cc = array ( $this -> email_format ( $enquiry [ 'User' ][ 'email' ]));
$this -> Email -> bcc = array ( $this -> email_format ( 'carpis@cmctechnologies.com.au' ));
2012-11-16 19:36:36 -08:00
$this -> Email -> replyTo = $enquiry [ 'User' ][ 'email' ];
$this -> Email -> from = 'CMC Technologies - Sales <sales@cmctechnologies.com.au>' ;
2012-12-17 03:50:19 -08:00
$docType = $this -> Document -> getDocType ( $document );
2016-04-30 19:46:16 -07:00
$template = $docType . '_email' ;
2016-04-30 20:14:01 -07:00
$subject = $enquiry [ 'Enquiry' ][ 'title' ] . ' ' ;
2022-07-06 22:32:20 -07:00
2012-12-17 04:48:53 -08:00
switch ( $docType ) {
case 'quote' :
2022-07-06 22:32:20 -07:00
$subject = " Quotation: " . $enquiry [ 'Enquiry' ][ 'title' ];
2012-12-17 04:48:53 -08:00
break ;
case 'invoice' :
2022-07-06 22:32:20 -07:00
$subject = $this -> invoice_email_subject ( $document );
2012-12-17 04:48:53 -08:00
$this -> set ( 'invoice' , $this -> Document -> Invoice -> find ( 'first' , array ( 'conditions' => array ( 'Invoice.id' => $document [ 'Invoice' ][ 'id' ]))));
2022-07-06 22:32:20 -07:00
$this -> set ( 'job' , $this -> Document -> Invoice -> Job -> find ( 'first' , array ( 'conditions' => array ( 'Job.id' => $document [ 'Invoice' ][ 'job_id' ]))));
2012-12-17 04:48:53 -08:00
break ;
case 'purchaseOrder' :
2022-07-06 22:32:20 -07:00
$subject .= " Purchase Order " ;
2016-05-24 03:13:45 -07:00
$primary_contact = $this -> Document -> User -> find ( 'first' , array ( 'conditions' => array ( 'User.principle_id' => $document [ 'PurchaseOrder' ][ 'principle_id' ], 'User.primary_contact' => 1 )));
if ( empty ( $primary_contact )) {
$this -> Session -> setFlash ( __ ( 'Unable to send. No primary Principle Contact' , true ));
$this -> redirect ( array ( 'action' => 'view/' . $id ), null , false );
return ;
}
2021-09-18 22:07:16 -07:00
2021-09-18 22:18:18 -07:00
$subject = $this -> po_email_subject ( $document [ 'PurchaseOrder' ]);
2016-12-09 15:24:54 -08:00
$this -> Email -> to = $this -> email_format ( $primary_contact [ 'User' ][ 'email' ]);
2016-05-24 03:13:45 -07:00
$this -> Email -> replyTo = null ;
$this -> Email -> cc = null ;
2012-12-17 04:50:20 -08:00
$this -> set ( 'job' , $this -> Document -> PurchaseOrder -> Job -> find ( 'first' , array ( 'conditions' => array ( 'Job.id' => $document [ 'OrderAcknowledgement' ][ 'job_id' ]))));
2012-12-17 04:48:53 -08:00
break ;
case 'orderAck' :
2022-06-25 17:05:18 -07:00
$subject = $this -> orderack_email_subject ( $document );
2012-12-17 04:48:53 -08:00
$this -> set ( 'job' , $this -> Document -> OrderAcknowledgement -> Job -> find ( 'first' , array ( 'conditions' => array ( 'Job.id' => $document [ 'OrderAcknowledgement' ][ 'job_id' ]))));
2016-04-30 19:46:16 -07:00
if ( $document [ 'OrderAcknowledgement' ][ 'signature_required' ]) {
2016-04-30 20:47:13 -07:00
$template = 'orderAck_email_signature_required' ;
2022-07-06 22:32:20 -07:00
}
2022-07-06 22:47:04 -07:00
case 'packingList' :
$subject .= " Packing List " ;
2012-12-17 04:48:53 -08:00
}
2012-12-17 03:50:19 -08:00
$this -> Email -> template = $template ;
2016-04-30 20:14:01 -07:00
$this -> Email -> subject = $subject ;
2012-12-17 03:50:19 -08:00
2012-11-16 19:36:36 -08:00
$this -> Email -> sendAs = 'both' ;
$this -> Email -> charset = 'iso-8859-1' ;
$this -> set ( 'enquiry' , $enquiry );
2012-12-17 04:27:44 -08:00
$this -> set ( 'document' , $document );
2012-11-16 19:36:36 -08:00
$this -> set ( 'DocFullName' , $this -> Document -> getDocFullName ( $document [ 'Document' ][ 'type' ]));
if ( $this -> Email -> send ()) {
$this -> Session -> setFlash ( __ ( 'The Email has been sent' , true ));
2012-11-18 12:19:55 -08:00
$this -> redirect ( array ( 'action' => 'view/' . $id ), null , false );
2011-09-17 02:33:17 -07:00
}
2012-11-16 19:36:36 -08:00
else {
$this -> set ( 'smtp_errors' , $this -> Email -> smtpError );
2022-01-07 18:12:40 -08:00
$this -> Session -> setFlash ( __ ( 'The Email has NOT been sent' , true ));
2020-11-03 00:35:26 -08:00
//$this->redirect(array('action'=>'view/'.$id), null, false);
2012-11-16 19:36:36 -08:00
}
}
2011-09-17 02:33:17 -07:00
2013-06-23 01:31:21 -07:00
function add_job_items_to_po ( $job_id , $document_id ) {
$this -> layout = 'ajax' ;
$job = $this -> Document -> PurchaseOrder -> Job -> find ( 'first' , array ( 'conditions' => array ( 'Job.id' => $job_id )));
$orderack = $this -> Document -> OrderAcknowledgement -> find ( 'first' , array ( 'conditions' => array ( 'OrderAcknowledgement.enquiry_id' => $job [ 'Job' ][ 'enquiry_id' ]), 'recursive' => 1 ));
$oa_document = $this -> Document -> find ( 'first' ,
array ( 'conditions' =>
array ( 'Document.id' => $orderack [ 'Document' ][ 'id' ]
)
)
);
$destination_document = $this -> Document -> find ( 'first' , array ( 'conditions' => array ( 'Document.id' => $document_id )));
$new_items = $this -> copy_related ( $oa_document , 'LineItem' , array ( 'id' , 'document_id' , 'costing_id' ));
$last_item_number = 1.0 ;
foreach ( $destination_document [ 'LineItem' ] as $item ) {
$last_item_number = $item [ 'item_number' ];
}
if ( $last_item_number != 1.0 ) { //Fuck knows
$last_item_number ++ ;
}
foreach ( $new_items as $i => $item ) {
$item [ 'item_number' ] = $last_item_number ++ ;
$item [ 'document_id' ] = $document_id ;
$destination_document [ 'LineItem' ][] = $item ;
}
if ( $this -> Document -> LineItem -> saveAll ( $destination_document [ 'LineItem' ])) {
echo 'SUCCESS' ;
}
else {
echo 'FAILURE' ;
}
}
2011-09-09 02:23:54 -07:00
2011-08-08 23:33:21 -07:00
2013-06-08 21:09:30 -07:00
/** Helper function to make copying related models more DRY
*
* @ param array $document
* @ param string $model - eg 'LineItem'
* @ param array $keys_to_null - eg . array ( 'id' , 'document_id' )
*
* */
function copy_related ( $document , $model , $keys_to_null ) {
if ( empty ( $document [ $model ])) {
return null ;
}
$newDoc [ $model ] = $document [ $model ];
foreach ( $newDoc [ $model ] as $index => $ele ) {
foreach ( $keys_to_null as $key ) {
$newDoc [ $model ][ $index ][ $key ] = null ;
}
}
return $newDoc [ $model ];
}
2011-08-08 23:33:21 -07:00
2010-07-07 23:18:04 -07:00
}