3 * Table Definition for cmhead
5 require_once 'DB/DataObject.php';
7 class Pman_Xtuple_DataObjects_Cmhead extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
12 public $__table = 'cmhead'; // table name
13 public $cmhead_id; // int4(4) not_null default_nextval%28%28cmhead_cmhead_id_seq%29%3A%3Aregclass%29 primary_key
14 public $cmhead_number; // text(-1)
15 public $cmhead_posted; // bool(1)
16 public $cmhead_invcnumber; // text(-1)
17 public $cmhead_custponumber; // text(-1)
18 public $cmhead_cust_id; // int4(4)
19 public $cmhead_docdate; // date(4)
20 public $cmhead_shipto_id; // int4(4)
21 public $cmhead_shipto_name; // text(-1)
22 public $cmhead_shipto_address1; // text(-1)
23 public $cmhead_shipto_address2; // text(-1)
24 public $cmhead_shipto_address3; // text(-1)
25 public $cmhead_shipto_city; // text(-1)
26 public $cmhead_shipto_state; // text(-1)
27 public $cmhead_shipto_zipcode; // text(-1)
28 public $cmhead_salesrep_id; // int4(4)
29 public $cmhead_freight; // numeric(-1)
30 public $cmhead_misc; // numeric(-1)
31 public $cmhead_comments; // text(-1)
32 public $cmhead_printed; // bool(1)
33 public $cmhead_billtoname; // text(-1)
34 public $cmhead_billtoaddress1; // text(-1)
35 public $cmhead_billtoaddress2; // text(-1)
36 public $cmhead_billtoaddress3; // text(-1)
37 public $cmhead_billtocity; // text(-1)
38 public $cmhead_billtostate; // text(-1)
39 public $cmhead_billtozip; // text(-1)
40 public $cmhead_hold; // bool(1)
41 public $cmhead_commission; // numeric(-1)
42 public $cmhead_misc_accnt_id; // int4(4)
43 public $cmhead_misc_descrip; // text(-1)
44 public $cmhead_rsncode_id; // int4(4)
45 public $cmhead_curr_id; // int4(4) default_basecurrid%28%29
46 public $cmhead_freighttaxtype_id; // int4(4)
47 public $cmhead_gldistdate; // date(4)
48 public $cmhead_billtocountry; // text(-1)
49 public $cmhead_shipto_country; // text(-1)
50 public $cmhead_rahead_id; // int4(4)
51 public $cmhead_taxzone_id; // int4(4)
52 public $cmhead_prj_id; // int4(4)
53 public $cmhead_billto_cntct_id; // int4(4)
54 public $cmhead_billto_addr_id; // int4(4)
55 public $cmhead_location_id; // extra...
59 * Getter / Setter for $cmhead_freighttaxtype_id
61 * @param mixed (optional) value to assign
64 public function freighttaxtype() {
65 return func_num_args() ? $this->link('cmhead_freighttaxtype_id', func_get_arg(0)) : $this->link('cmhead_freighttaxtype_id');
69 * Getter / Setter for $cmhead_prj_id
71 * @param mixed (optional) value to assign
74 public function prj() {
75 return func_num_args() ? $this->link('cmhead_prj_id', func_get_arg(0)) : $this->link('cmhead_prj_id');
79 * Getter / Setter for $cmhead_taxzone_id
81 * @param mixed (optional) value to assign
84 public function taxzone() {
85 return func_num_args() ? $this->link('cmhead_taxzone_id', func_get_arg(0)) : $this->link('cmhead_taxzone_id');
89 * Getter / Setter for $cmhead_curr_id
91 * @param mixed (optional) value to assign
94 public function curr() {
95 return func_num_args() ? $this->link('cmhead_curr_id', func_get_arg(0)) : $this->link('cmhead_curr_id');
99 * Getter / Setter for $cmhead_billto_cntct_id
101 * @param mixed (optional) value to assign
104 public function billto_cntct() {
105 return func_num_args() ? $this->link('cmhead_billto_cntct_id', func_get_arg(0)) : $this->link('cmhead_billto_cntct_id');
109 * Getter / Setter for $cohead_salesrep_id
111 * @param mixed (optional) value to assign
114 public function salesrep() {
115 return func_num_args() ? $this->link('cmhead_salesrep_id', func_get_arg(0)) : $this->link('cmhead_salesrep_id');
118 /* the code above is auto generated do not remove the tag below */
121 function defaults() {
123 $sa = DB_DataObject::Factory('salesaccnt');
124 $sa->salesaccnt_custtype ='.*';
125 $sa->salesaccnt_prodcat = '.*';
130 'cmhead_printed'=>true,
131 'cmhead_rsncode_id'=> $this->sqlValue('NULL'),
132 'cmhead_posted'=> false,
133 'cmhead_custponumber' => '',
134 'cmhead_freight' => 0.0,
135 'cmhead_misc' => 0.0,
136 'cmhead_hold' => false,
137 'cmhead_commission' => 0.0,
138 'cmhead_misc_accnt_id' => $sa->salesaccnt_credit_accnt_id,
139 'cmhead_misc_descrip' => '',
140 'cmhead_freighttaxtype_id' => $this->sqlValue('NULL'),
141 'cmhead_rahead_id' => $this->sqlValue('NULL'),
142 'cmhead_prj_id'=> $this->sqlValue('NULL'),
145 //public $cmhead_invcnumber; // text(-1)
146 //public $cmhead_cust_id; // int4(4)
147 //public $cmhead_docdate; // date(4)
148 //public $cmhead_shipto_id; // int4(4)
149 //public $cmhead_salesrep_id; // int4(4)
150 //public $cmhead_freight; // numeric(-1)
151 //public $cmhead_misc; // numeric(-1)
152 //public $cmhead_comments; // text(-1)
153 //public $cmhead_curr_id; // int4(4) default_basecurrid%28%29
154 //public $cmhead_taxzone_id; // int4(4)
156 //public $cmhead_billto_cntct_id; // int4(4)
157 //public $cmhead_billto_addr_id; // int4(4)
158 //public $cmhead_location_id; // extra...
164 function applyFilters($q, $au, $roo)
166 //join the customer table
167 //DB_DataObject::debugLevel(1);
168 $tn= $this->tableName();
170 //$ci = DB_DataObject::Factory('custinfo');
171 //$this->selectAs($ci, 'cmhead_cust_id_%s');
172 //$this->_join .= " LEFT JOIN custinfo ON cmhead_cust_id=custinfo.cmhead_cust_id ";
174 //join the value from cmitem table as cmhead_value
177 cmhead_taxzone_id != gettaxzoneid('NO TAX')
179 (SELECT SUM(cmitem_unitprice * cmitem_qtyreturned +
185 cmitem_unitprice * cmitem_qtyreturned
190 cmitem_cmhead_id = cmhead.cmhead_id
194 SUM(cmitem_unitprice * cmitem_qtyreturned)
198 cmitem_cmhead_id = cmhead.cmhead_id
206 cmhead_taxzone_id != gettaxzoneid('NO TAX')
209 select COALESCE( sum(
215 cmitem_unitprice * cmitem_qtyreturned
221 cmitem_cmhead_id = cmhead.cmhead_id
223 cmitem_taxtype_id = gettaxtypeid('Taxable')
227 END as cmhead_tax_value,
230 cmhead_taxzone_id != gettaxzoneid('NO TAX')
233 select COALESCE( sum(
234 cmitem_unitprice * cmitem_qtyreturned
240 cmitem_cmhead_id = cmhead.cmhead_id
242 cmitem_taxtype_id = gettaxtypeid('Taxable')
246 END as cmhead_taxable_value,
250 cmhead_taxzone_id != gettaxzoneid('NO TAX')
253 select COALESCE( sum(
254 cmitem_unitprice * cmitem_qtyreturned
260 cmitem_cmhead_id = cmhead.cmhead_id
262 cmitem_taxtype_id != gettaxtypeid('Taxable')
266 END as cmhead_taxfree_value
270 $this->selectAdd("gettaxtypeid('Taxable') AS default_taxtype_id");
272 //join the bill contact and address
274 $cntct = DB_DataObject::factory('cntct');
276 LEFT JOIN cntct AS billcntct ON ( cmhead_billto_cntct_id = billcntct.cntct_id)
278 $this->selectAs($cntct, 'cmhead_billto_cntct_id_%s', 'billcntct');
280 $addr = DB_DataObject::Factory('addr');
282 LEFT JOIN addr AS billaddr ON ( cmhead_billto_addr_id = billaddr.addr_id)
284 $this->selectAs($addr, 'cmhead_billto_cntct_id_cntct_addr_id_%s', 'billaddr');
287 //get the location name
289 /*$loc = DB_DataObject::factory('location');
291 distinct(cmhead_location_id) as cmhead_location_id,
292 (SELECT location_name from location where location_id = cmhead_location_id) as cmhead_location_name
297 $tax = DB_DataObject::factory('taxzone');
301 COALESCE( (SELECT taxrate_percent FROM taxrate WHERE taxrate_tax_id IN
302 (SELECT taxass_tax_id FROM taxass WHERE
303 taxass_taxzone_id = cmhead.cmhead_taxzone_id
304 AND taxrate_effective < '$d' AND taxrate_expires > '$d'
306 ), 0) as taxzone_rate
311 (SELECT aropen_amount - aropen_paid FROM
316 aropen_ordernumber = cmhead.cmhead_number
323 if (!empty($q['query']['cmhead_number'])) {
324 //DB_DataObject::DebugLevel(1);
325 $this->whereAdd("{$tn}.cmhead_number ilike '" . $this->escape($q['query']['cmhead_number']) . "%'");
329 $this->joinAddCustSalesRep();
331 if (!empty($q['query']['status']))
333 $ap = "(SELECT aropen_open FROM aropen WHERE aropen_doctype ='C' and aropen_ordernumber = cmhead.cmhead_number LIMIT 1)";
334 //DB_DataObject::DebugLevel(1);
335 // if it's been used..
336 switch($q['query']['status']) {
339 {$tn}.cmhead_posted = false OR
347 {$tn}.cmhead_posted = true AND
353 $this->cmhead_posted = false;
358 {$tn}.cmhead_posted = true
365 $this->whereAdd(" NOT {$ap} ");
369 $this->whereAdd($ap);
376 if(isset($q['_with_aropen'])){
377 $this->joinAddAropen();
380 (join_cmhead_aropen_id.aropen_amount - join_cmhead_aropen_id.aropen_paid -
382 COALESCE(SUM(checkhead_amount),0)
386 ((checkhead_id=checkitem_checkhead_id)
388 (NOT checkhead_posted)
392 (checkitem_aropen_id=aropen_id)))
400 function joinAddCustSalesRep()
403 LEFT JOIN salesrep AS join_cust_salesrep_id_salesrep_id
404 ON ( join_cust_salesrep_id_salesrep_id.salesrep_id = join_cmhead_cust_id_cust_id.cust_salesrep_id)
406 $this->selectAs(DB_DataObject::Factory('salesrep'), 'cust_salesrep_%s', 'join_cust_salesrep_id_salesrep_id');
411 function beforeUpdate($old, $q,$roo){
414 $this->contact2bill($q);
415 if (isset($q['cmhead_posted']) && $q['cmhead_posted'] == 'false') {
416 $this->cmhead_posted = false;
418 if (!empty($q['_post'])) {
419 $this->post($roo); // should only return if it succeeds.
420 $roo->jok($this->pid());
422 if (!empty($q['_void'])) {
423 $this->void($roo); // should only return if it succeeds.
426 if (empty($this->cmhead_misc_accnt_id)) {
429 $sa = DB_DataObject::Factory('salesaccnt');
430 $sa->salesaccnt_custtype ='.*';
431 $sa->salesaccnt_prodcat = '.*';
433 $this->cmhead_misc_accnt_id = $sa->salesaccnt_credit_accnt_id;
438 function beforeInsert($request,$roo)
440 if(isset($request['_fix_stock'])){
441 $cmhead = DB_DataObject::factory('cmhead');
442 $cmhead->query("SELECT invfifo_apply_gl_cmhead_fix_stock()");
447 if (isset($request['_is_xfer'])) {
448 $this->recieveXfer($roo, $request);
449 $this->jerr("no ok");
452 // handle automatic numbering..
453 if ($this->cmhead_number == 'Automatic' || empty($this->cmhead_number)) {
454 $this->cmhead_number = $this->nextNumber();
456 $this->cmhead_posted = false;
457 $this->contact2bill($request);
460 function beforeDelete()
462 $cmitem = DB_DataObject::factory('cmitem');
464 cmitem_cmhead_id = {$this->pid()}
466 $cmitem->delete(DB_DATAOBJECT_WHEREADD_ONLY);
469 function items($what=false)
471 $i = $this->factory('cmitem');
472 $i->cmitem_cmhead_id = $this->cmhead_id;
474 //$i->orderBy('cmitem_linenumber ASC');
475 return $i->fetchAll($what);
478 function relatedWhere()
481 'cmitem' => $this->items('cmitem_id'),
491 function contact2bill($request){
493 $bill = $this->billto_cntct();
494 //print_r($bill);exit;
496 if ($bill->cntct_id) {
497 $this->setFrom($bill->toArray('cmhead_billto_%s'));
498 $this->cmhead_billto_cntct_id = $bill->cntct_id;
501 $addr = $bill->addr();
502 //print_r($addr);exit;
503 $this->cmhead_billto_addr_id = $addr->addr_id;
504 $this->setFrom(array(
505 'cmhead_billtoname' => $bill->cntct_name,
506 'cmhead_billtoaddress1' => $addr->addr_line1,
507 'cmhead_billtoaddress2' => $addr->addr_line2,
508 'cmhead_billtoaddress3' => $addr->addr_line3,
509 'cmhead_billtocity' => $addr->addr_city,
510 'cmhead_billtostate' => $addr->addr_state,
511 'cmhead_billtozipcode' => $addr->addr_postalcode,
512 'cmhead_billtocountry' => $addr->addr_country,
517 function toRooArray($req)
520 $ret = $this->toArray();
521 // seaching for order id's does not include id..
522 if (!$this->cmhead_id) {
526 $l = $this->factory('cmhead');
529 calcsalesordersubtotal(cmhead_id::integer) as cmhead_subtotal,
530 calcsalesordertax(cmhead_id::integer) as cmhead_tax
532 $l->get($this->pid());
533 $x = $l->toArray('%s', true);
538 function nextNumber()
540 //DB_DataObject::debugLevel(1);
541 $cp = DB_DataObject::factory('cmhead');
542 $date = substr(date('Y'), -2);
544 $pr = "CM-". strtoupper(substr($this->database(), -2)).$date."-";
545 $cp->whereAdd("cmhead_number LIKE '$pr%'");
548 $cp->orderBy('cmhead_number DESC');
550 $res = $cp->fetchAll('cmhead_number');
556 $np = substr($last,strlen($pr)) *1;
559 return $pr . sprintf('%04d', ($np+1));
564 $tn = $this->tableName();
565 // check we are ready to go..
566 if (empty($this->cmhead_location_id)) {
567 $roo->jerr("no location provided");
569 if (!empty($this->cmhead_posted)) {
570 $roo->jerr("credit memo is already posted");
573 // we have created a cmhead..
575 $db = DB_DataObject::factory($tn);
576 $db->query("SELECT postCreditMemo({$ret}, 0) as result");
578 if (empty($db->result) || $db->result < 0) {
579 $roo->jerr("postCreditMemo failed with result being {$db->result}! " .
580 print_r($ret,true) );
583 $itemlocdist_series = $db->result;
585 // print_r("\nDEBUG: {$itemlocdist_series} \n");
587 $db = DB_DataObject::factory($tn);
588 $db->query("SELECT itemlocdist_id,
589 itemlocdist_reqlotserial,
590 itemlocdist_distlotserial,
593 itemsite_controlmethod,
596 COALESCE(itemsite_lsseq_id,-1) AS itemsite_lsseq_id,
597 COALESCE(itemlocdist_source_id,-1) AS itemlocdist_source_id
598 FROM itemlocdist, itemsite
599 WHERE ( (itemlocdist_itemsite_id=itemsite_id)
600 AND (itemlocdist_series={$itemlocdist_series}) )
601 ORDER BY itemlocdist_id
604 while ($db->fetch()) {
605 if (empty($db->itemlocdist_id) || $db->itemlocdist_id < 0) {
606 die("itemlocdist_id isn't found! \n");
608 $itemlocdist_id = $db->itemlocdist_id;
610 $sdb = DB_DataObject::factory($tn);
611 $sdb->query("INSERT INTO itemlocdist (itemlocdist_itemlocdist_id,
612 itemlocdist_source_type,
613 itemlocdist_source_id,
615 itemlocdist_ls_id, itemlocdist_expiration
617 SELECT itemlocdist_id,
619 {$this->cmhead_location_id},
624 WHERE (itemlocdist_id={$itemlocdist_id})
628 $sdb = DB_DataObject::factory($tn);
629 $sdb->query("SELECT distributeToLocations({$itemlocdist_id}) AS result");
633 $db = DB_DataObject::factory($tn);
634 $db->query("SELECT postItemlocseries({$itemlocdist_series}) AS result");
639 $loc = DB_DataObject::Factory('location');
640 if (!$loc->get($this->cmhead_location_id)) {
641 $roo->jerr("no location specified");
644 $cust = $loc->customer();
646 $roo->jerr("the location specified does not have a customer associated with it.");
648 $our_db = substr($this->database(),-2);
651 $loc_db = $cust->char('INTERNALCOMPANY');
653 $loc_db = empty($loc_db) ? $our_db : $loc_db ;
655 // this now needs to create a credit memo in HK to transfer the stock back to HK..
657 // transfer if necessary..
658 if ($our_db != $loc_db) {
659 if (HTML_FlexyFramework::get()->cli) {
660 $roo->jerr("CLI can not be used to do transfer");
663 $res = $this->doStockTransfer($roo);
665 // rollback handled by top level..
666 //$s->query("ROLLBACK");
679 // end handling items being posted..
681 // SG ONLY ... - creates a matching credit memo in HK..
684 * this is for voiding a un-apply credit memo
685 * voidcreditmemo(integer)
686 * pCmheadid ALIAS FOR $1;
691 $tn = $this->tableName();
693 if (empty($this->cmhead_posted)) {
694 $roo->jerr("credit memo is not posted");
698 // check who can do this..
699 $admin = DB_DataObject::factory('groups')->lookup('name', 'Administrators' );
701 ($this->cmhead_salesrep_id != $roo->authUser->salesrep()->pid())
703 (!in_array($roo->authUser->pid() , $admin->memberIds()))
706 $roo->jerr("you may not void this credit memo - you must be the sales rep or an administrator");
713 $aropen = DB_DataObject::factory('aropen');
714 $aropen->aropen_docnumber = $this->cmhead_number;
715 $aropen->doctype = 'C';
716 if(!$aropen->find(true)){
717 $roo->jerr("Error occur on finding aropen id for {$this->cmhead_number}");
720 $arapply = DB_DataObject::factory('arapply');
722 arapply_target_aropen_id = {$aropen->pid()}
724 arapply_source_aropen_id = {$aropen->pid()}
727 if($arapply->find(true)){
728 $roo->jerr("Can not void {$this->cmhead_number} since it has applied to application");
731 $this->checkLocationStock($roo);
735 $db = DB_DataObject::factory($tn);
736 $db->query("SELECT voidCreditMemo({$ret}) as result");
738 if (empty($db->result) || $db->result < 0) {
739 $roo->jerr("voidCreditMemo failed with result being {$db->result}! " . print_r($ret,true) );
742 $itemlocdist_series = $db->result;
743 //$roo->jerr("$itemlocdist_series");
750 $db = DB_DataObject::factory($tn);
751 $db->query("SELECT itemlocdist_id,
752 itemlocdist_reqlotserial,
753 itemlocdist_distlotserial,
756 itemsite_controlmethod,
759 COALESCE(itemsite_lsseq_id,-1) AS itemsite_lsseq_id,
760 COALESCE(itemlocdist_source_id,-1) AS itemlocdist_source_id
761 FROM itemlocdist, itemsite
762 WHERE ( (itemlocdist_itemsite_id=itemsite_id)
763 AND (itemlocdist_series={$itemlocdist_series}) )
764 ORDER BY itemlocdist_id
767 while ($db->fetch()) {
768 if (empty($db->itemlocdist_id) || $db->itemlocdist_id < 0) {
769 die("itemlocdist_id isn't found! \n");
771 $itemlocdist_id = $db->itemlocdist_id;
773 $sdb = DB_DataObject::factory($tn);
774 $sdb->query("INSERT INTO itemlocdist (itemlocdist_itemlocdist_id,
775 itemlocdist_source_type,
776 itemlocdist_source_id,
778 itemlocdist_ls_id, itemlocdist_expiration
780 SELECT itemlocdist_id,
782 {$this->cmhead_location_id},
787 WHERE (itemlocdist_id={$itemlocdist_id})
791 $sdb = DB_DataObject::factory($tn);
792 $sdb->query("SELECT distributeToLocations({$itemlocdist_id}) AS result");
798 // this generates an additional itemlocdist record..
803 $db = DB_DataObject::factory($tn);
804 $db->query("SELECT postItemlocseries({$itemlocdist_series}) AS result");
814 $query = " select * from invhist where invhist_comments like 'Credit Voided%' and not invhist_hasdetail ";
816 // fetch the CM-head based on ordnumber
818 // look up in itemloc -> what the current qty is..
820 // search for invdetail - with same doc number invdetail_qty > 0 - distinct locatoin_id
821 //if more than 1.. error out.. and let's check them;
824 INSERT INTO invdetail
825 ( invdetail_invhist_id, invdetail_location_id, invdetail_ls_id,
826 invdetail_qty, invdetail_qty_before, invdetail_qty_after, invdetail_expiration,
831 SELECT _itemlocdist.invhistid, itemloc_location_id, itemloc_ls_id,
832 _itemlocdist.qty, itemloc_qty, (itemloc_qty + _itemlocdist.qty),
833 itemloc_expiration,_itemlocdist.warranty
835 WHERE (itemloc_id=_itemlocid)
840 -- Update the parent invhist to indicate that it has invdetail records
842 SET invhist_hasdetail=TRUE
843 WHERE ((invhist_hasdetail=FALSE)
844 AND (invhist_id=_itemlocdist.invhistid));
850 function doStockTransfer($roo)
852 $roo->jerr("cross company transfer disabled");
853 $items = $this->items();
856 $items = $this->sellToHK($roo, $items); // remove the stock so that HK can have it..
859 $ff = HTML_FlexyFramework::get();
861 if (empty($ff->Xtuple['main_url'])) {
862 $roo->jerr("Xtuple['main_url'] is not configured");
864 if (empty($ff->Xtuple['main_remote_cust_number'])) {
865 //$roo->jerr( "Xtuple['main_remote_cust_number'] is not configured");
866 $roo->jerr("Old code using remote_customer - disabled");
872 $loc = DB_DataObject::Factory('location');
873 $loc->get($this->cmhead_location_id);
881 // Order number (so it can be voided later..)
882 // list of items (and the source location)
884 // UnitPrice?? - this should be the FIFO cost..
887 // on the signapore site
889 $full = DB_DataObject::Factory('cmhead');
891 $full->get($this->pid());
893 $roo->jerr("Old code using remote_customer - disabled");
895 require_once 'HTTP/Request.php';
897 $req = new HTTP_Request( $ff->Xtuple['main_url'] . '/Roo/Cmhead' );
898 $req->setMethod(HTTP_REQUEST_METHOD_POST);
901 $base = $this->toArray();
902 unset($base['cmhead_id']);
903 unset($base['cmhead_id']);
905 $req->addPostData( array(
909 'cmhead_number' => 'XF-' . $full->cmhead_number,
910 'cust_number' => $ff->Xtuple['main_remote_cust_number'], // $roo->jerr("Old code using remote_customer - disabled");
911 'cmhead_docdate' => $full->cmhead_docdate,
914 'cmhead_freight' => 0 , //$full->cmhead_freight ,
915 'cmhead_misc' => 0, //$full->cmhead_misc,
917 'cmhead_comments' => $full->cmhead_comments,
918 //'cmhead_misc_descrip'=> '', //$full->cmhead_misc_descrip,
920 'location_name' => $full->cmhead_location_id_location_name,
921 //'cmhead_misc_accnt_id_accnt_number' => $full->cmhead_misc_accnt_id_accnt_number,
924 //'cmhead_curr_id; // int4(4) default_basecurrid%28%29
925 //cmhead_freighttaxtype_id; // int4(4)
926 //public $cmhead_salesrep_id; // int4(4)
928 //cmhead_taxzone_id; // int4(4)
935 $res = $req->sendRequest();
936 if (is_a($res,'PEAR_Error')) {
937 $roo->jerr( "MAIN REQUEST RETURNED: ". $res->toString());
939 $res = json_decode($req->getResponseBody());
943 if (!is_object($res)) {
944 $roo->jerr("MAIN REQUEST RETURNED: ". $req->getResponseBody());
947 if (!$res->success) {
948 $roo->jerr("MAIN REQUEST RETURNED: ". $res->errorMsg);
956 function sellToHK($roo,$items)
959 $ff = HTML_FlexyFramework::get();
962 foreach($items as $item) {
964 $xitem = $item->itemsite()->item();
965 if (!isset($item_req[$xitem ->item_number])) {
966 $item_req[$xitem ->item_number] = 0;
968 $item_req[$xitem->item_number] += $item->cmitem_qtycredit;
972 'linenumber'=> $item->cmitem_linenumber,
973 'item_number'=> $xitem->item_number,
974 'qty' => $item->cmitem_qtycredit
981 $po = DB_DataObject::factory('pohead');
983 //print_R($item_req);exit;
985 $prices = $po->fetchXferPrices($roo, $item_req);
987 // now read the data and fill in the values..
988 foreach($out as $i=>$item) {
990 if (!isset($prices[$item['item_number']])) {
991 $roo->jerr( "ERROR MAIN REQUEST FAILED TO RETURN PRICE FOR {$item['item_number']}");
993 $out[$i]['unitprice'] = $prices[$item['item_number']];
996 $loc = DB_DataObject::Factory('location');
997 $loc->get($this->cmhead_location_id);
999 //print_R($out);exit;
1001 $cust = DB_DataObject::Factory('custinfo');
1002 $cust ->get('cust_number', $ff->Xtuple['main_vendor_number']);
1004 $curr= DB_DataObject::Factory('curr_symbol');
1005 $curr->get('curr_abbr', 'HKD');
1007 // now make a debit memo... for the transaction..
1008 $d = DB_DataObject::Factory('invchead');
1009 $d->handleXfer($roo, array(
1012 'invchead_invcnumber' => 'XF-'.$this->cmhead_number, // invoice should match number...
1013 'invchead_orderdate' => $this->cmhead_docdate,
1014 'invchead_invcdate' => $this->cmhead_docdate,
1015 'cust_number' => $cust->cust_number,
1016 'invchead_curr_id' => $curr->pid(),
1017 'invchead_posted' => false,
1018 'invchead_printed' => false,
1019 'invchead_commission' => 0,
1020 'invchead_freight' => 0,
1021 'invchead_misc_amount' => 0,
1022 'invchead_shipchrg_id' => -1,
1024 'src_location' => $loc->location_name,
1036 // in HK... we recieved a transfer CM..
1037 function recieveXfer($roo, $r)
1040 //$roo->jerr("xfer testing");
1041 $cm = DB_DataObject::factory('cmhead');
1044 foreach($cm->defaults() as $k=>$v) {
1045 if (!isset($r[$k])) {
1050 if (empty($r['location_name'])) {
1051 $roo->jerr("no location name recieved.");
1053 $loc = DB_DataObject::Factory('location');
1054 if (!$loc->get('location_name', $r['location_name'])) {
1055 $roo->jerr("could not find lcoation {$r['location_name']}");
1057 $cm->cmhead_location_id = $loc->pid();
1060 if (empty($r['cust_number'])) {
1061 $roo->jerr("no cust_number recieved.");
1063 $cust = DB_DataObject::Factory('custinfo');
1064 if (!$cust->get('cust_number', $r['cust_number'])) {
1065 $roo->jerr("could not find customer number : {$r['cust_number']}");
1068 $cm->cmhead_cust_id = $cust->pid();
1073 foreach($r['items'] as $item) {
1074 $i = DB_DataObject::Factory('cmitem');
1075 $i->recieveXfer($roo,$cm, $item);
1081 // fill in our varous data...
1084 $roo->jok("transfered");
1090 function joinAddAropen()
1092 $aropen = DB_DataObject::factory('aropen');
1094 LEFT JOIN aropen join_cmhead_aropen_id
1096 join_cmhead_aropen_id.aropen_doctype = 'C'
1098 join_cmhead_aropen_id.aropen_docnumber = cmhead_number
1100 join_cmhead_aropen_id.aropen_cust_id = cmhead_cust_id
1105 $this->selectAs($aropen, 'cmhead_aropen_id_%s', 'join_cmhead_aropen_id');
1108 function checkLocationStock($roo)
1110 if(empty($roo->bootLoader->Xtuple['prevent_negative'])){
1114 $items = $this->items();
1118 foreach ($items as $item){
1119 if(empty($item->cmitem_qtycredit)){
1123 $balance = $item->itemsite()->checkLocationStock($this->cmhead_location_id);
1125 if(empty($balance) || $balance < $item->cmitem_qtycredit){
1126 $stock[] = $item->itemsite()->item()->item_number;
1131 $roo->jerr("These items have negative stock " . implode(', ', $stock));