column('name','value'); foreach($quotation_detail as &$v){ $v['tax_inclusive_amount'] = $v['num'] * $v['tax_inclusive_price']; $v['tax_exclusive_amount'] = $v['num'] * $v['tax_inclusive_price'] * (1- $tax_rate_dict[$v['tax_rate']] / 100); } Db::startTrans(); try { $quotation = Quotation::create([ 'custom_id' => $params['custom_id'], 'code' => data_unique_code('报价单'), 'quotation_date' => !empty($params['quotation_date']) ? strtotime($params['quotation_date']) : 0, 'create_user' => $params['create_user'] ?? '', 'invoice_type' => $params['invoice_type'] ?? 0, 'freight' => $params['freight'], 'other_fee' => $params['other_fee'], 'customer_require' => $params['customer_require'] ?? '', 'remark' => $params['remark'] ?? '', 'annex' => $params['annex']? json_encode($params['annex']) : null, ]); foreach ($quotation_detail as $item) { QuotationDetail::create([ 'quotation_id' => $quotation->id, 'product_id' => $item['product_id'], 'num' => $item['num'], 'tax_rate' => $item['tax_rate'], 'tax_inclusive_price' => $item['tax_inclusive_price'], 'tax_inclusive_amount' => $item['tax_inclusive_price'] * $item['num'], 'tax_exclusive_amount' => $item['tax_inclusive_price'] * $item['num'] * (1- $tax_rate_dict[$item['tax_rate']] / 100), 'remark' => $item['remark'] ?? '' ]); } Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } public static function edit(array $params): bool { $quotation_detail = $params['quotation_detail']; $tax_rate_dict = DictData::where('type_value','tax_rate')->column('name','value'); foreach($quotation_detail as &$v){ $v['tax_inclusive_amount'] = $v['num'] * $v['tax_inclusive_price']; $v['tax_exclusive_amount'] = $v['num'] * $v['tax_inclusive_price'] * (1- $tax_rate_dict[$v['tax_rate']] / 100); } Db::startTrans(); try { $quotation = Quotation::where('id',$params['id'])->update([ 'custom_id' => $params['custom_id'], 'quotation_date' => !empty($params['quotation_date']) ? strtotime($params['quotation_date']) : 0, 'create_user' => $params['create_user'] ?? '', 'invoice_type' => $params['invoice_type'] ?? 0, 'freight' => $params['freight'], 'other_fee' => $params['other_fee'], 'customer_require' => $params['customer_require'] ?? '', 'remark' => $params['remark'] ?? '', 'annex' => $params['annex']? json_encode($params['annex']) : null, 'update_time' => time(), ]); foreach ($quotation_detail as $item) { if(!empty($item['id'])){ QuotationDetail::where('id',$item['id'])->update([ 'quotation_id' => $params['id'], 'product_id' => $item['product_id'], 'num' => $item['num'], 'tax_rate' => $item['tax_rate'], 'tax_inclusive_price' => $item['tax_inclusive_price'], 'tax_inclusive_amount' => $item['tax_inclusive_price'] * $item['num'], 'tax_exclusive_amount' => $item['tax_inclusive_price'] * $item['num'] * (1- $tax_rate_dict[$item['tax_rate']] / 100), 'remark' => $item['remark'] ?? '' ]); }else{ QuotationDetail::create([ 'quotation_id' => $params['id'], 'product_id' => $item['product_id'], 'num' => $item['num'], 'tax_rate' => $item['tax_rate'], 'tax_inclusive_price' => $item['tax_inclusive_price'], 'tax_inclusive_amount' => $item['tax_inclusive_price'] * $item['num'], 'tax_exclusive_amount' => $item['tax_inclusive_price'] * $item['num'] * (1- $tax_rate_dict[$item['tax_rate']] / 100), 'remark' => $item['remark'] ?? '' ]); } } Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } public static function delete(array $params): bool { $detail = QuotationDetail::where('quotation_id','in',$params['id'])->findOrEmpty(); if(!$detail->isEmpty()){ self::setError('此数据关联了报价明细信息,须删除报价明细信息'); return false; } return Quotation::destroy($params['id']); } /** * @notes 获取报价单详情 * @param $params * @return array * @author likeadmin * @date 2023/11/27 17:23 */ public static function detail($params): array { $data = Quotation::field('id,custom_id,quotation_date,create_user,invoice_type,freight,other_fee,customer_require,remark,annex')->findOrEmpty($params['id']); $custom = Custom::field('name,master_name,master_phone')->where('id',$data['custom_id'])->findOrEmpty(); $data['custom_name'] = $custom['name']; $data['custom_master_name'] = $custom['master_name']; $data['custom_master_phone'] = $custom['master_phone']; $data['invoice_type_text'] = $data->invoice_type_text; $data['amount_including_tax'] = QuotationDetail::where('quotation_id',$data['id'])->sum('tax_inclusive_amount'); $data['total_amount'] = $data['amount_including_tax'] + $data['freight'] + $data['other_fee']; return $data->toArray(); } public static function datas() { return Quotation::field(['id','code'])->order(['id' => 'desc'])->select()->each(function($data){ $data['projectinfo'] = 'ID:' . $data['id'] . ' / 单号:' . $data['code']; })->toArray(); } }