/** * Store a newly created resource in storage. * * @param StoreInvoiceRequest request * @return Response */ public function store(StoreInvoiceRequest $request) { $input = $request->only('agent_id', 'edition_id', 'issue_date'); // Validate requests before invoice was created. Such as: // - Agent has not yet billed // - Deliveries exist // // //get distribution plan executed for this agent and particular // magazine for this date $distReal = DistReal::with('details.agent', 'edition.magazine')->where('edition_id', '=', $input['edition_id'])->first(); // only get distRealDet where agent_id matches foreach ($distReal->details as $det) { if ((int) $det->agent_id == (int) $input['agent_id']) { $distRealDet = $det; break; } } //get necessary delivery for this month $deliveries = Delivery::with('distRealizationDet.distributionRealization.edition')->whereRaw('dist_realization_det_id = ? AND in_invoice_quota = 0', [$distRealDet->id])->get(); //get returns for this month and 3 months ago with the same magazine $ed = Edition::with('magazine')->find((int) $input['edition_id']); //generate necessary data to go to invoice //generate data to create invoice $inv['num'] = $this->createInvNumber(); $inv['number'] = "{$distReal->edition->magazine->id}/" . str_pad($inv['num'], 5, 0, STR_PAD_LEFT); $inv['agent_id'] = $input['agent_id']; $issueDate = strtotime($input['issue_date']); $inv['issue_date'] = date('Y-m-d', $issueDate); //MySQL format $dueDate = strtotime('last friday of this month'); $inv['due_date'] = date('Y-m-d', $dueDate); $inv['edition_id'] = (int) $input['edition_id']; // Invoice OK $newInv = InvoiceQuota::firstOrCreate($inv); // generate invoice delivery details foreach ($deliveries as $delv) { $toDB['delivery_id'] = $delv->id; $toDB['invoice_quota_id'] = $newInv->id; // Calculating price here // Current edition price, multiplied by quota amount $toDB['total'] = $delv->distRealizationDet->distributionRealization->edition->price * $delv->quota; $toDB['edition_id'] = $input['edition_id']; $newInvDelivery = InvDelivery::firstOrCreate($toDB); //Update in_invoice to true $delv->in_invoice_quota = 1; $delv->save(); } $msg = "Done! New Invoice# : {$newInv->number}"; return redirect("invoice/invoice")->with('message', $msg); }
/** * return true if item are returnable * * Return criterias are: * - return number is not existed before * - sum is less than or equal to total delivered item * * @param Array input from form validation * @param int edition_id * @param int total returned amount on form * * @return int distRealDetID if passed all exceptions */ private function validateReturnItem($input, $edition_id, $total) { // Check if return.num already existed $existed = ReturnItem::where('num', '=', (int) $input['number'])->get(); if (!$existed->isEmpty()) { $err = "Return number is already existed! Return#: {$existed->implode('number')}"; throw new \Exception($err); } // Check if agent_id match to distribution_plan $realizationDetail = DistRealDet::with('distributionRealization')->where('agent_id', '=', $input['agent_id'])->get(); if ($realizationDetail->isEmpty()) { $err = 'No delivery was made for this agent'; throw new \Exception($err); } $distRealDetID = 0; // look for each distributionPlan foreach ($realizationDetail as $rd) { if ($rd->distributionRealization->edition_id == (int) $edition_id) { $distRealDetID = (int) $rd->id; } } if ($distRealDetID == 0) { $err = 'No editions were delivered for this agent'; throw new \Exception($err); } //Previous returns $returnAmount = 0; $prevRets = ReturnItem::where('dist_realization_det_id', '=', $distRealDetID)->get(); if ($prevRets) { foreach ($prevRets as $x) { $returnAmount += $x->total; } } $total = $returnAmount + $total; // Delivery amount is less than return $deliveryAmount = 0; $deliveries = Delivery::where('dist_realization_det_id', '=', $distRealDetID)->get(); if ($deliveries) { foreach ($deliveries as $x) { // Quota cannot be returned, so use consigned amount $deliveryAmount += $x->consigned; } } if ($deliveryAmount < $total) { //Must fail $err = "Mismatched return amount!" . " Deliveries made = {$deliveryAmount}." . " Total returned amount = {$total}."; throw new \Exception($err); } return $distRealDetID; }
/** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { // try { $delivery = Delivery::findOrFail($id); $delivery->delete(); $execMsg = "Delete Successful!"; } catch (ModelNotFoundException $e) { $execMsg = "Cannot delete record. Data not found."; return redirect('circulation/delivery')->with('errMsg', $execMsg); } return redirect('circulation/delivery')->with('message', $execMsg); }
/** * Store a newly created resource in storage. * * @param Request request * @return Response */ public function store(StoreInvoiceRequest $request) { $input = $request->only('agent_id', 'edition_id', 'issue_date'); // Validate requests before invoice was created. Such as: // - Agent has not yet billed // - Deliveries exist // // //get distribution plan executed for this agent and particular // magazine for this date $distReal = DistReal::with('details.agent', 'edition.magazine')->where('edition_id', '=', $input['edition_id'])->first(); // only get distRealDet where agent_id matches foreach ($distReal->details as $det) { if ((int) $det->agent_id == (int) $input['agent_id']) { $distRealDet = $det; break; } } //get necessary delivery for this month $deliveries = Delivery::with('distRealizationDet.distributionRealization.edition')->whereRaw('dist_realization_det_id = ? AND in_invoice_consign = 0', [$distRealDet->id])->get(); //get returns for this month and 3 months ago with the same magazine $ed = Edition::with('magazine')->find((int) $input['edition_id']); // just use scopeInvoice (see: http://laravel.com/docs/5.0/eloquent#query-scopes) $returns = ReturnItem::with('distRealizationDet.distributionRealization.edition')->Invoice((int) $input['agent_id'], (int) $ed->magazine->id)->get(); //generate necessary data to go to invoice //generate data to create invoice $inv['num'] = $this->createInvNumber(); $inv['number'] = "{$distReal->edition->magazine->id}/" . str_pad($inv['num'], 5, 0, STR_PAD_LEFT); $inv['agent_id'] = $input['agent_id']; $issueDate = strtotime($input['issue_date']); $inv['issue_date'] = date('Y-m-d', $issueDate); //MySQL format $dueDate = strtotime('last friday of this month'); $inv['due_date'] = date('Y-m-d', $dueDate); $inv['edition_id'] = (int) $input['edition_id']; // Invoice OK $newInv = InvoiceConsign::firstOrCreate($inv); // generate invoice delivery details foreach ($deliveries as $delv) { $toDB['delivery_id'] = $delv->id; $toDB['invoice_consign_id'] = $newInv->id; // Calculating price here // Current delivery price, multiplied by consign $toDB['total'] = $delv->distRealizationDet->distributionRealization->edition->price * $delv->consigned; $toDB['edition_id'] = $input['edition_id']; $newInvDelivery = InvDelivery::firstOrCreate($toDB); //Update in_invoice to true $delv->in_invoice_consign = 1; $delv->save(); } // Because all details will have the same edition $thisMonthPrice = $delv->distRealizationDet->distributionRealization->edition->price; unset($toDB); foreach ($returns as $ret) { $toDB['return_item_id'] = $ret->id; $toDB['invoice_consign_id'] = $newInv->id; // Calculating price here. We will calculate discount based on: // thisMonthPrice * total return - returnMonthPrice * total return $returnMonthPrice = $ret->distRealizationDet->distributionRealization->edition->price; $toDB['discount'] = $ret->total * ($thisMonthPrice - $returnMonthPrice); // of course, total will use thisMonthPrice, even though returned // item may have different price. $toDB['total'] = $thisMonthPrice * $ret->total; $toDB['edition_id'] = $input['edition_id']; $newInvDelivery = InvReturn::firstOrCreate($toDB); //Update in_invoice $ret->in_invoice = 1; $ret->save(); } $msg = "Done! New Invoice# : {$newInv->number}"; return redirect("invoice/invoice")->with('message', $msg); }