/**
* Returns the data model based on the primary key given in the GET variable.
* If the data model is not found, an HTTP exception will be raised.
* @param integer the ID of the model to be loaded
*/
public function loadModel($id)
{
$model=Orderproductdn::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
Esempio n. 2
0
 private function addorupdateOrderLineItems($order, $postedData, &$rolproducts)
 {            
     $extorderprds = $order->orderproducts;
     $postedorderprds = $postedData->orderproducts;
     //check for deleted orderproducts.
     $todelete = array();
     foreach($extorderprds as $eop){
         $found = false;
         foreach($postedorderprds as $pop){
             $found = ($eop->id === $pop->id);
             if($found)
             {
                 break;
             }
         }
         if(!$found){
             $todelete[] = $eop;
         }
     }
     foreach($todelete as $dop){
         $dopid = $dop->id;
         $prdid = $dop->product_id;
         if($this->enableordrdn){
             $conditions = array('orderproduct_id=?', $dopid);
             $dopdns = \Orderproductdn::find('all', array('conditions' => $conditions));
             if(isset($dopdns) && is_array($dopdns)){
                foreach($dopdns as $dopdn){
                    $dopdn->delete();                           
                }
             }
         }
         $dop->delete();                
         if($this->enablestock){
             $prd = $dop->product;
             //$prd = \Product::find($prdid);
             if(!$prd){
                 continue;
             }
             $qnty = $dop->delivered;
             $newstock= $prd->stock + $qnty;
             $attributes = array(
                 "stock" => $newstock,
                 "stockvalue" => $newstock * $prd->unit_cp);
             $prd->update_attributes($attributes);
         }
     }
     $ototalcost = 0;
     $ototalamount = 0;
     $ototaltaxamount = 0;
     $ototaldiscper = 0;
     $ototaldisc = 0;
     foreach($postedorderprds as $ordprod){
         $qnty = $ordprod->quantity;
         if(!$this->enableordrdn){
             $ordprod->delivered = $ordprod->quantity;
         }else if(!isset($ordprod->delivered)){
             $ordprod->delivered = 0;
         }
         
         $vldqnty = (is_numeric($qnty) && $qnty > 0);
         if(!$vldqnty){
             continue;
         }
         $prd = \Product::find($ordprod->product_id);
         if(!$prd){
             continue;
         }
         if($prd->rol >= ($prd->stock - $qnty)){
             $rolproducts[] = $prd->name;
         }
         $ucost = $prd->unit_cp;
         $ugainper = $prd->gainpercent;//actual unit gain percentage
         $ugain = $ucost * $ugainper / 100;//actual unit gain amount
         $ugain = round($ugain, 2);
         $usp = $ucost + $ugain;//actual unit selling price
         $usp = round($usp, 2);
         
         $cost = $qnty * $ucost;//actual total cost
         $ttlamnttobe = $qnty * $usp;//actual total selling price                                
         $postedamount = (isset($ordprod->amount) && is_numeric($ordprod->amount))?$ordprod->amount:$ttlamnttobe;
         
         $discper = 0;
         $disc = 0;
         if($order->enablediscount === 1 && ($order->orderdiscfor === 0 || $order->orderdiscfor === 3)){
             if($this->discentry === 1){
                 if($postedamount !== $ttlamnttobe){
                     $disc = $ttlamnttobe - $postedamount;
                     $discper = $disc / $ttlamnttobe * 100.0;
                 }
             }else if($this->discentry === 0){
                 $discper = (isset($ordprod->discper) && is_numeric($ordprod->discper))?$ordprod->discper:0;
                 $disc = $ttlamnttobe * $discper / 100.0;
                 //if discount amount entered but percent is 0. calculate %.
                 $disctmp = (isset($ordprod->disc) && is_numeric($ordprod->disc))?$ordprod->disc:0;
                 if($discper <= 0 && $disctmp > 0){
                     $discper = $disctmp / $ttlamnttobe * 100.0;
                 }
             }
         }
         $taxamount = $postedamount * $prd->taxper / 100 ;
         $attributes = array(
                 "order_id" => $order->id,
                 "order_type" => $order->type,
                 "product_id" => $ordprod->product_id,
                 "quantity" => $qnty,
                 "delivered" => ((!$this->enableordrdn)?$qnty:$ordprod->delivered),
                 "cost" => $cost,
                 "unit_sp" => $ordprod->unit_sp,
                 "amount" => $postedamount,
                 "tax" => $taxamount,
                 "discper" => $discper,
                 "disc" => $disc,
                 );
         $id = $ordprod->id;
         if(!isset($id)){
             $id = -1;
         }
         try{
             $data = \Orderproduct::find($id);
         }catch (\ActiveRecord\RecordNotFound $ex) {
             $data = null;
         }
         if(is_null($data)){
             $data = \Orderproduct::create($attributes);
             if($this->enablestock && !$this->enableordrdn){
                 $newstock= $prd->stock - $qnty;
             }
         }else{
             $prvqnty = $data->quantity;
             $data->update_attributes($attributes);
             if($this->enablestock && !$this->enableordrdn){
                 $newstock= $prd->stock - $qnty;//minus current posted.
                 $newstock= $newstock + $prvqnty;//plus qnty prvsly deducted.
             }
         }
         $ototalcost += $cost;
         $ototalamount += $postedamount;
         $ototaltaxamount += $taxamount;
         $ototaldiscper += $discper;
         $ototaldisc += $disc;
         if($this->enablestock && !$this->enableordrdn){
             $attributes = array(
                 "stock" => $newstock,
                 "stockvalue" => $newstock * $prd->unit_cp);
             $prd->update_attributes($attributes);
         }
     }
     if((!is_null($order) && $order->ordercostamountfrom === 1) 
             || $postedData->ordercostamountfrom === 1){
         //updating order amounts with lineitems
         $otaxper = $ototaltaxamount / $ototalamount * 100.0;//average tax % on this order
         if($order->enablediscount === 1 
                 && ($order->orderdiscfor === 0 
                 || $order->orderdiscfor === 3)
         ){
             if($this->discentry === 1){
                 $postedamount = (isset($postedData->amount) && is_numeric($postedData->amount))?$postedData->amount:0;
                 if($postedamount !== $ototalamount){
                     $ototaldisc = $ototalamount - $postedamount + $ototaldisc;                            
                 }
                 $ototaldiscper = round($ototaldisc / $ttlamnttobe * 100.0, 2);
                 $ototalamount = $postedamount;
             }else if($this->discentry === 0){
                 $tmpdiscper = (isset($order->discper) && is_numeric($order->discper))?$order->discper:0;                        
                 $ototaldisc += round($ototalamount * $tmpdiscper / 100.0, 2);
                 $ototaldiscper = $ototaldisc / $ttlamnttobe * 100.0;
                 //if discount amount entered but percent is 0. calculate %.
                 $disctmp = (isset($order->disc) && is_numeric($order->disc))?$order->disc:0;
                 if($ototaldiscper <= 0 && $disctmp > 0){
                     $ototaldisc += $disctmp;
                     $ototaldiscper = $ototaldisc / $ttlamnttobe * 100.0;
                 }
                 $ototaldiscper = round($ototaldiscper, 2);
                 $ototalamount = $ttlamnttobe - $ototaldisc;
             }
         }else{
             $ototaldisc = 0;
             $ototaldiscper = 0;
         }
         $this->updateAmntTaxInOrder($order, $ototalcost, $ototalamount, $otaxper, $ototaltaxamount, $ototaldiscper, $ototaldisc);
     }
     if($this->enableordrdn){
         $orderagain = \Order::find($order->id);
         $this->updateOrderDeliveryStatus($orderagain);
     }
 }